Quantcast
Channel: sql-server – Tech ABC to XYZ
Viewing all articles
Browse latest Browse all 30

Must declare the table variable "@temp" [ANSWERED]

$
0
0

Must declare the table variable "@temp"

Asked By: Abhishek
Originally Asked On: 2014-01-02 12:01:41
Asked Via: stackoverflow

I create a stored procedure in which I am using a dynamic query. The query will get data from my query and insert into my temp table and in last I select data from @temp table.

This is my stored procedure:

    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

When I execute I get error message

Msg 1087, Level 15, State 2, Line 1
Must declare the table variable “@temp”.

What am I doing wrong in this query?

Regards

He received 1 answers
eventually accepting:

Devart’s answer to

Must declare the table variable "@temp"

Try to use temporary tables –

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

If the selected answer did not help you out, the other answers might!

All Answers For: Must declare the table variable "@temp"

Devart’s answer to

Must declare the table variable "@temp"

Try to use temporary tables –

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

Of course, you should really check out the original question.

The post Must declare the table variable "@temp" [ANSWERED] appeared first on Tech ABC to XYZ.


Viewing all articles
Browse latest Browse all 30

Trending Articles