必须声明表变量“@temp”

Posted

技术标签:

【中文标题】必须声明表变量“@temp”【英文标题】:Must declare the table variable "@temp" 【发布时间】:2014-01-02 12:01:41 【问题描述】:

我创建了一个使用动态查询的存储过程。该查询将从我的查询中获取数据并插入到我的临时表中,最后我从@temp 表中选择数据。

这是我的存储过程:

    ALTER PROCEDURE uspSearchEmployee 
@Name varchar(50)= null,
@EmpNumber varchar(50)=null,
@Location Varchar(50)=null,
@position varchar(50)=null,
@partialmatch bit ,
@partialmatch2 bit,
@partialmatch3 bit 

AS
BEGIN
declare @wheresql varchar(max)


if (@Name is not null)
BEGIN
if(@partialmatch=1)
 set @wheresql=' where EmpName like ''%'+  @Name + ''''
else
  set @wheresql=' where EmpName = ' +@Name
END
IF(@Name is null AND @EmpNumber IS NOT NULL )
BEGIN
if(@partialmatch2=1)
 set @wheresql=' where EmployeeNum like ''%' + @EmpNumber + ''''
else
  set @wheresql=' where EmployeeNum = '+@EmpNumber
END 
IF(@Name is NOT null AND @EmpNumber IS NOT NULL )
BEGIN
if(@partialmatch2=1)
 set @wheresql=@wheresql+' AND EmployeeNum like ''%'+@EmpNumber +''''
else
  set @wheresql=@wheresql+' AND EmployeeNum ='+@EmpNumber
END 
--3rd case
IF(@Name is null AND @EmpNumber IS NULL AND @Location IS NOT NULL )
BEGIN
if(@partialmatch3=1)
 set @wheresql=' where Location like ''%' +@Location + ''''
else
  set @wheresql=' where Location = ' +@Location
END 
IF((@Name is NOT null OR @EmpNumber IS NOT NULL) AND @Location IS NOT NULL )
BEGIN
if(@partialmatch3=1)
 set @wheresql=@wheresql+ ' AND Location like ''%'+@EmpNumber + ''''
else
  set @wheresql=@wheresql+' AND Location ='+@EmpNumber
END 
IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
        DROP TABLE #temp
 CREATE TABLE #temp          
(        
  EmpId int,
  EmpName varchar(40),
  Location varchar(50)null,
  City varchar(40)null,
  STPROV varchar(15)null,
  EmergencyPhone varchar(50)null,
  ZIPPOSTAL varchar(10) null,
  Home_Phone varchar(25)null,
  Country varchar(25) null,
  Department varchar(50) null,
  Position varchar(50) null,
  WorkCompCode varchar(50) null,
  Active bit null        
)  
set @wheresql=' INSERT INTO #temp
                SELECT  EmpId,EmpName,Location,City,STPROV,EmergencyPhone,ZIPPOSTAL,Home_Phone,Country,Department
                Position,WorkCompCode,Active 
                from employee ' + ISNULL(@wheresql, '')  
 PRINT  @wheresql  
  EXECUTE  sys.sp_executesql @wheresql
 Select * FROM  #temp


END

当我执行时,我收到错误消息

消息 1087,第 15 级,状态 2,第 1 行 。

我在这个查询中做错了什么?

问候

【问题讨论】:

变量仅在当前范围内定义。 execute(@sql) 为执行的 sql 创建新的作用域。 您现在已经完全更改了问题中的代码,不是吗,因为它不再有意义?存储过程中根本没有@temp 表变量,但周围的文本和错误消息仍然引用它。如果代码仍然存在问题,您需要编辑问题的其余部分。如果您显示的代码没有有问题,请恢复到损坏的代码。问题旨在在未来几年内保持可用,如果问题不再显示错误,它只会让人们感到困惑。投票和接受答案是如何完成的。 【参考方案1】:

尝试使用临时表 -

ALTER PROCEDURE dbo.uspSearchEmployee

    @Name VARCHAR(50) = NULL,
    @EmpNumber VARCHAR(50) = NULL,
    @Location VARCHAR(50) = NULL,
    @position VARCHAR(50) = NULL,
    @partialmatch BIT,
    @partialmatch2 BIT,
    @partialmatch3 BIT

AS BEGIN

    SET NOCOUNT ON;
    DECLARE @SQL NVARCHAR(MAX)

    IF @Name IS NOT NULL
    BEGIN
        IF (@partialmatch = 1)
            SET @SQL = ' WHERE EmpName LIKE ''%' + @Name + ''''
        ELSE
            SET @SQL = ' WHERE EmpName = ' + @Name
    END

    IF @Name IS NULL AND @EmpNumber IS NOT NULL
    BEGIN
        IF (@partialmatch2 = 1)
            SET @SQL = ' WHERE EmployeeNum LIKE ''%' + @EmpNumber + ''''
        ELSE
            SET @SQL = ' WHERE EmployeeNum = ' + @EmpNumber

        IF (@partialmatch2 = 1)
            SET @SQL = @SQL + ' AND EmployeeNum LIKE ''%' + @EmpNumber + ''''
        ELSE
            SET @SQL = @SQL + ' AND EmployeeNum =' + @EmpNumber
    END

    IF @Name IS NULL AND @EmpNumber IS NULL AND @Location IS NOT NULL
    BEGIN
        IF (@partialmatch3 = 1)
            SET @SQL = ' WHERE Location LIKE ''%' + @Location + ''''
        ELSE
            SET @SQL = ' WHERE Location = ' + @Location
    END

    IF (@Name IS NOT NULL OR @EmpNumber IS NOT NULL) AND @Location IS NOT NULL
    BEGIN
        IF (@partialmatch3 = 1)
            SET @SQL = @SQL + ' AND Location LIKE ''%' + @EmpNumber + ''''
        ELSE
            SET @SQL = @SQL + ' AND Location =' + @EmpNumber
    END

    IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
        DROP TABLE #temp

    CREATE TABLE #temp 
    (
        EmpId INT,
        EmpName VARCHAR(40),
        Location VARCHAR(50) NULL,
        City VARCHAR(40) NULL,
        STPROV VARCHAR(15) NULL,
        EmergencyPhone VARCHAR(50) NULL,
        ZIPPOSTAL VARCHAR(10) NULL,
        Home_Phone VARCHAR(25) NULL,
        Country VARCHAR(25) NULL,
        Department VARCHAR(50) NULL,
        Position VARCHAR(50) NULL,
        WorkCompCode VARCHAR(50) NULL,
        Active BIT NULL
    )

    SET @SQL = '
    INSERT INTO #temp
    SELECT EmpId,EmpName,Location,City,STPROV,EmergencyPhone,ZIPPOSTAL,Home_Phone,Country,Department,Position,WorkCompCode,Active 
    FROM dbo.employee ' + ISNULL(@SQL, '')

    PRINT @SQL
    EXEC sys.sp_executesql @SQL

    SELECT * FROM #temp

END

【讨论】:

@Davart 给出的错误Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1 Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'. 这里如何使用 if (@Name 不为空) IF @Name IS NULL 等于 ISNULL(@SQL, '') 你能看看我更新的问题吗

以上是关于必须声明表变量“@temp”的主要内容,如果未能解决你的问题,请参考以下文章

表变量和错误“必须声明标量变量”

必须声明表变量 "@P0"

SQL 提示必须声明表变量

SQL 错误 - 必须声明表变量“@tablename”

从 SQL 函数返回动态透视表的困难 - “必须声明表变量 xxx”

更新另一个表时必须声明标量变量@tablename [重复]