Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

sp_executesql : Trying to make sense of OUTPUT param

$
0
0
Good day,I am having difficulty understanding how to make available a parameter set with dynamic T-SQL.I expect value of the [font="Courier New"]@s_MaxOrderOut[/font] to be already defined in the [font="Courier New"]SELECT @s_MaxOrderOut = MAX(...[/font]But the last line of the [font="Courier New"]sp_excutesql [/font]statement appears to replace this parameter's value with the (undefined, thus NULL) value of the "destination" parameter defined outside of the list of parameters defined for the sp_executesql:[font="Courier New"]...@s_MaxOrderOut = @s_Max_SO_No OUTPUT; ???[/font]Instead of copying the [font="Courier New"]@s_MaxOrderOut[/font] value to the destination parameter [font="Courier New"]@s_Max_SO_No OUTPUT[/font].How does that T-SQL statement read ? - sort of right-argument equal sign left-argument ?[code="sql"]CREATE TABLE #t( SalesOrderID [int] IDENTITY(1,1) NOT NULL, CustomerID int, SalesOrderNumber varchar (25)) ;INSERT INTO #t (CustomerID, SalesOrderNumber)VALUES (100, 12300), (100, 12399), (101, 12350) ;DECLARE @n_SQL nvarchar(MAX) = N'SELECT @s_MaxOrderOut = MAX(SalesOrderNumber) FROM #t WHERE CustomerID = @i_CID' ;DECLARE @n_Params nvarchar(MAX) = N'@i_CID int, @s_MaxOrderOut varchar(25) OUTPUT' ;DECLARE @i_Cust int = 100 ;DECLARE @s_Max_SO_No varchar(25) ;EXECUTE sp_executesql @n_SQL, @n_Params, @i_Cust, [b]@s_MaxOrderOut = @s_Max_SO_No OUTPUT[/b]; SELECT @s_Max_SO_No;[/code]

Viewing all articles
Browse latest Browse all 3145

Trending Articles