动态SQL不工作 - 符合,但错误

时间:2014-10-22 17:50:12

标签: sql-server tsql

CREATE TABLE [dbo].[TSQL_DLOOKUP](
    [LookUpTable] [nchar](10) NULL,
    [LookUpField] [nchar](10) NULL,
    [LookUpValue] [nchar](10) NULL,
    [LookUpDecode] [nchar](20) NULL
) ON [PRIMARY]

GO


The procedure:

ALTER PROCEDURE [dbo].[MyDLookUp]
@LookUpValue NCHAR(10),
@LookUpTable NCHAR(10),
@LookUpField NCHAR(10)
AS
BEGIN
DECLARE @SQLQuery AS NVARCHAR(500)
DECLARE @ParameterDefinition AS NVARCHAR(100)
/* Build Transact-SQL String by including the parameter */
SET @SQLQuery = 'SELECT LookUpDecode FROM TSQL_DLOOKUP WHERE LookUpTable = @LookUpTable AND LookUpValue = @LookUpValue AND LookUpField =@LookUpField' 
/* Execute Transact-SQL String */
EXECUTE sp_executesql @SQLQuery, @LookUpValue,@LookUpTable,@LookUpField
END

enter code here要运行:

    DECLARE @return_value int

    EXEC    @return_value = [dbo].[MyDLookUp]
            @LookUpValue = N'F',
            @LookUpTable = N'LCLASS',
            @LookUpField = N'CLASS'


 SELECT 'Return Value' = @return_value


    The error: Msg 102, Level 15, State 1, Line 1
    Incorrect syntax near 'F'.
    Msg 137, Level 15, State 2, Line 1
    Must declare the scalar variable "@LookUpTable".



The code compiles, but will not run. Please help.

这里的想法是尽可能准确地复制MSACCESS DLOOOKUP功能。我正在进行一个项目,其中存在大量重复的SQL语句,其中我之前的编码器就是这样做的,我需要简化一些事情。

4 个答案:

答案 0 :(得分:3)

正如LAMAK所说,显然不需要动态查询,你可以通过使用staic查询来做同样的事情!!

SELECT lookupdecode 
FROM   tsql_dlookup 
WHERE  lookuptable = @LookUpTable 
       AND lookupvalue = @LookUpValue 
       AND lookupfield = @LookUpField 

执行相同工作的功能

CREATE FUNCTION dlookup(@LookUpValue NCHAR(10),
@LookUpTable NCHAR(10),
@LookUpField NCHAR(10))
RETURNS nchar
AS
BEGIN
    declare @lookupdecode nchar(50)
      SELECT  top 1 @lookupdecode = lookupdecode 
    FROM   tsql_dlookup 
    WHERE  lookuptable = @LookUpTable 
           AND lookupvalue = @LookUpValue 
           AND lookupfield = @LookUpField 

   RETURN @lookupdecode 
END;

答案 1 :(得分:2)

您缺少sp_executesql命令中的@params参数,该命令定义了传入的参数:

EXECUTE sp_executesql @SQLQuery, 
    N'@LookUpValue [nchar](10),@LookUpTable [nchar](10),@LookUpField [nchar](10)',
    @LookUpValue,@LookUpTable,@LookUpField

答案 2 :(得分:1)

正如其他人所说,在这种情况下不需要动态sql,因为它可以直接执行。

要回答您的问题,请参阅下文。

参数定义需要作为参数传递给sp_executesql SP,以将参数传递给动态查询http://msdn.microsoft.com/en-us/library/ms188001.aspx

因此,您的新SP将如下

alter PROCEDURE [dbo].[MyDLookUp]
@LookUpValue NCHAR(10),
@LookUpTable NCHAR(10),
@LookUpField NCHAR(10)
AS
BEGIN
DECLARE @SQLQuery AS NVARCHAR(500)
DECLARE @ParameterDefinition AS NVARCHAR(100)
/* Build Transact-SQL String by including the parameter */
SET @SQLQuery = 'SELECT LookUpDecode FROM TSQL_DLOOKUP WHERE LookUpValue = @LookUpValue AND LookUpTable = @LookUpTable AND  LookUpField =@LookUpField' 

DECLARE @ParmDefinition nvarchar(500);

SET @ParmDefinition = N'@LookUpValue NCHAR(10), @LookUpTable NCHAR(10), @LookUpField NCHAR(10)';

/* Execute Transact-SQL String */
EXECUTE sp_executesql @SQLQuery, @ParmDefinition, @LookUpValue,@LookUpTable,@LookUpField
END

答案 3 :(得分:0)

尝试使用此代码,如果这对您有用,因为不需要使用动态SQL来进行简单的选择状态

---------------------------------------存储过程

    Alter PROCEDURE [dbo].[MyDLookUp] --[dbo].[MyDLookUp] 1,2,3
    @LookUpValue NCHAR(10),
    @LookUpTable NCHAR(10),
    @LookUpField NCHAR(10)
    AS
    BEGIN        

    SELECT LookUpDecode FROM TSQL_DLOOKUP 
    WHERE LookUpTable = @LookUpTable AND LookUpValue = @LookUpValue AND LookUpField =@LookUpField

    END