% Rowtype equivalent in SQL Server

I am going to convert a stored procedure from pl / sql to SQL Server. The procedure uses the cursor to process the results of the select query. Is SQL Server equivalent to an ORACLE rowtype ?

+5
source share
3 answers

This is a serious reason you don't like SQL Server compared to oracle.

I am so disappointed that SS does not have% TYPE and% ROWTYPE. This allows you to save hours of work during the initial writing of the code and, if necessary, changing the type of the column, this eliminates the need to return and process all the code.

In Oracle, I wrote something like:

DECLARE @MyRowVar AS ATable% ROWTYPE;

In SQL Server, I just had to write the following:

 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; 

It is very sad.

Harvey

+17
source

The way to use the SQL cursor is shown below.

 DECLARE @colA varchar(50), @colB varchar(50) DECLARE myCursor CURSOR FOR Select columnA, columnB From table OPEN myCursor FETCH NEXT FROM myCursor INTO @colA, @colB WHILE @@FETCH_STATUS = 0 BEGIN --do something with @colA and @colB FETCH NEXT FROM myCursor INTO @colA, @colB END CLOSE myCursor DEALLOCATE myCursor 

link link

+4
source

As @HarveyFrench replied, no @rowtype . To generate all the column variables, you can use this selection and copy the paste from the results:

 SELECT 'DECLARE @customPrefix' + COLUMN_NAME + ' ' + DATA_TYPE + ';' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Your_table' 
+1
source

All Articles