必须声明表变量“@temp”

时间:2014-01-02 12:01:42

标签: sql-server sql-server-2008 stored-procedures

我创建了一个存储过程,我在其中使用动态查询。查询将从我的查询中获取数据并插入到临时表中,最后我从@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

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

  

Msg 1087,Level 15,State 2,Line 1
  必须声明表变量“@temp”。

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

此致

1 个答案:

答案 0 :(得分: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