2010-10-01 3 views
83

Ausführen von dynamischen SQL wie in Stored Procedure folgt:Erste Folge der dynamischen SQL in eine Variable für SQL-Server

DECLARE @sqlCommand nvarchar(1000) 
DECLARE @city varchar(75) 
SET @city = 'London' 
SET @sqlCommand = 'SELECT COUNT(*) FROM customers WHERE City = @city' 
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75)', @city = @city 

Wie verwende ich den count (*) Spaltenwert als Rückgabewert in der SP?

Antwort

136
DECLARE @sqlCommand nvarchar(1000) 
DECLARE @city varchar(75) 
declare @counts int 
SET @city = 'New York' 
SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city' 
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75),@cnt int OUTPUT', @city = @city, @[email protected] OUTPUT 
select @counts as Counts 
+2

+1: Sie schlagen mich dazu, müssen eine Variable deklarieren und als OUTPUT markieren. [Weitere Informationen und eine empfohlene Version für SQL Server Dynamic SQL finden Sie unter Der Fluch und Segen des dynamischen SQL] (http://www.sommarskog.se/dynamic_sql.html#sp_executesql) –

+1

Vielen Dank. Das Schlüsselwort OUTPUT in N '@ city nvarchar (75), @ cnt int OUTPUT' war das, was mir fehlte. –

+0

Gibt es keine Lösung, die das Hinzufügen einer Ausgangsvariablen zur dynamischen Anweisung nicht erfordert ??? –

2

Sie haben dies wahrscheinlich versucht, aber sind Ihre Spezifikationen so, dass Sie dies tun können?

DECLARE @city varchar(75) 
DECLARE @count INT 
SET @city = 'London' 
SELECT @count = COUNT(*) FROM customers WHERE City = @city 
+2

Oder: SELECT @count = COUNT (*) FROM Kunden WHERE Stadt = @city – Sage

+0

Sage Kommentar tatsächlich das bevorzugte Format ist. –

+0

@Sage, @OMG Ponys: guten Ruf. Es ist eine sehr unterschiedliche Programmierung in einer Textbox im Internet. :) – Brad

0
DECLARE @sqlCommand nvarchar(1000) 
DECLARE @city varchar(75) 
DECLARE @cnt int 
SET @city = 'London' 
SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city' 
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75)', @city = @city 
RETURN @cnt 
+1

Ich denke, deine Antwort wurde abgeschnitten. – Sage

-1
vMYQUERY := 'SELECT COUNT(*) FROM ALL_OBJECTS WHERE OWNER = UPPER(''MFI_IDBI2LIVE'') AND OBJECT_TYPE = ''TABLE'' 
    AND OBJECT_NAME =''' || vTBL_CLIENT_MASTER || ''''; 
    PRINT_STRING(VMYQUERY); 
    EXECUTE IMMEDIATE vMYQUERY INTO VCOUNTTEMP ; 
+0

Dies gilt nicht für SQL-Server. –

0

dynamische Version

ALTER PROCEDURE [dbo].[ReseedTableIdentityCol](@p_table varchar(max))-- RETURNS int 
    AS 
    BEGIN 
     -- Declare the return variable here 
     DECLARE @sqlCommand nvarchar(1000) 
     DECLARE @maxVal INT 
     set @sqlCommand = 'SELECT @maxVal = ISNULL(max(ID),0)+1 from '[email protected]_table 
     EXECUTE sp_executesql @sqlCommand, N'@maxVal int OUTPUT',@[email protected] OUTPUT 
     DBCC CHECKIDENT(@p_table, RESEED, @maxVal) 
    END 


exec dbo.ReseedTableIdentityCol @p_table='Junk' 
0

dies eine Lösung sein könnte?

declare @step2cmd nvarchar(200) 
DECLARE @rcount NUMERIC(18,0) 
set @step2cmd = 'select count(*) from uat.ap.ztscm_protocollo' --+ @nometab 
EXECUTE @rcount=sp_executesql @step2cmd 
select @rcount