Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%Rowtype equivalent in SQL Server

I'm about to convert a stored procedure from pl/sql to SQL Server. The procedure uses a cursor to loop through the results of a select query. Is there a SQL Server equivalent to the ORACLE rowtype construct?

like image 358
Luca Martini Avatar asked Oct 26 '10 09:10

Luca Martini


People also ask

What is %type and %Rowtype in SQL?

The %ROWTYPE attribute, used to declare PL/SQL variables of type record with fields that correspond to the columns of a table or view, is supported by the Db2® data server. Each field in a PL/SQL record assumes the data type of the corresponding column in the table. A record is a named, ordered collection of fields.

What is the %Rowtype attribute used for?

The %ROWTYPE attribute provides a record type that represents a row in a database table. The record can store an entire row of data selected from the table or fetched from a cursor or cursor variable. Variables declared using %ROWTYPE are treated like those declared using a datatype name.

What is %Rowtype and %type?

%TYPE. %ROWTYPE. The %Type datatype is use to define the variable as column name datatype for specified table. If you dont know the datatype of specified column and you require to assign that datatype to the variable then you can use %ROWTYPE.

What is difference between Rowtype and type record?

%rowtype is an attribute to inherit datatypes of attributes of a table into a RECORD variable. Type record is a keyword to create record type using either explicitly specifying atrributes or by implecitly inheriting attributes from a table or a existing cursor.


1 Answers

This is a massive reason to dislike SQL Server when compared to oracle.

I am so disappointed that SS doesn't have %TYPE and %ROWTYPE. These save hours of work when initially writing code and in the event that the type of a column needs to change, it saves having to go back and re-work all the code.

In Oracle I used to write something like:

DECLARE @MyRowVar AS ATable%ROWTYPE;

In SQL Server, I just had to write this this:

DECLARE @External_ID                AS BIGINT = NULL;
DECLARE @PlatformID                 AS INT = NULL;
DECLARE @ActorIDOfReseller          AS INT = NULL;
DECLARE @ActorIDOfClient            AS INT = NULL;
DECLARE @ActorIDOfExtension         AS INT = NULL;
DECLARE @CallType                   AS NCHAR (10) = NULL;
DECLARE @CallInitiatedDate          AS DATE = NULL;
DECLARE @CallInitiatedTimeHH24MI    AS TIME (0) = NULL;
DECLARE @TimePeriodID               AS INT = NULL;
DECLARE @CallAnswered               AS DATETIME = NULL;
DECLARE @CallAnsweredYN             AS BIT = NULL;
DECLARE @CallDispositionID          AS INT = NULL;
DECLARE @CountryID                  AS INT = NULL;
DECLARE @CallPrefixID               AS INT = NULL;
DECLARE @FromNumber                 AS VARCHAR (32) = NULL;
DECLARE @ToNumber                   AS VARCHAR (80) = NULL;
DECLARE @CallDuration               AS INT = NULL;
DECLARE @CallCostToExtension        AS DECIMAL (10, 6) = NULL;
DECLARE @CallCostToClient           AS DECIMAL (10, 6) = NULL;
DECLARE @CallCostToReseller         AS DECIMAL (10, 6) = NULL;
DECLARE @CallCostToAdmin            AS DECIMAL (10, 6) = NULL;
DECLARE @Flow                       AS VARCHAR (3) = NULL;
DECLARE @CallStart                  AS DATETIME = NULL;
DECLARE @MoneyUnit                  AS VARCHAR (32) = NULL;
DECLARE @Prefix                     AS VARCHAR (32) = NULL;
DECLARE @External_CallID            AS VARCHAR (255) = NULL;

This is making me very sad.

Harvey

like image 179
HarveyFrench Avatar answered Oct 08 '22 03:10

HarveyFrench