我在存储过程中有大查询。在单个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
答案 0 :(得分:0)
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;