簡體   English   中英

存儲過程中的動態查詢

[英]Dynamic query in stored procedure

我在存儲過程中有很大的查詢。 在單個SQL查詢中無法執行全部代碼。 因此,我已經使用了兩個動​​態查詢。 當我嘗試在代碼中間打印或執行時,缺少某些行。 我很困惑應該在哪里確切地啟動新的Query1或可以執行整個查詢的任何其他方式:

CREATE Proc [dbo].[spGetRPTTot]
    (//parameters
    )
AS
BEGIN
    DECLARE @sqlQuery VARCHAR(MAX)

    SET @sqlQuery = ' //code '

    DECLARE @sqlQuery1 VARCHAR(MAX)
    SET @sqlQuery1 = ' //code '

    PRINT (@sqlQuery + @sqlQuery1)
    EXECUTE (@sqlQuery + @sqlQuery1)
END
GO

這是文檔中的示例。 https://docs.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/sp-executesql-transact-sql

DECLARE @IntVariable int;  
DECLARE @SQLString nvarchar(500);  
DECLARE @ParmDefinition nvarchar(500);  

/* Build the SQL string one time.*/  
SET @SQLString =  
     N'SELECT BusinessEntityID, NationalIDNumber, JobTitle, LoginID  
   FROM AdventureWorks2012.HumanResources.Employee   
   WHERE BusinessEntityID = @BusinessEntityID';  
SET @ParmDefinition = N'@BusinessEntityID tinyint';  
/* Execute the string with the first parameter value. */  
SET @IntVariable = 197;  
EXECUTE sp_executesql @SQLString, @ParmDefinition,  
                  @BusinessEntityID = @IntVariable;  
/* Execute the same string with the second parameter value. */  
SET @IntVariable = 109;  
EXECUTE sp_executesql @SQLString, @ParmDefinition,  
                  @BusinessEntityID = @IntVariable;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM