0

Can someone check what is wrong with this codes? I already check the other questions for reference but its still not working.

enter image description here

declare @sourceTable varchar(500)
declare @year varchar(22)
declare @month varchar(3)
declare @test varchar(12)
declare @result varchar(8)
declare @index int

declare @string varchar(15)
set @string = (SELECT DISTINCT TOP 1 REPLACE(dbo.fn_Parsename(WHOLEROW, '|', 0), CHAR(9), '') FROM #temp1)
set @test = (select UPPER(convert(datetime,substring(@string,2,charindex('-',@string,1)-2))))
set @month =(left(@test,3))

set @year = (right(@test,5))

set @result = @month + @year
-- select @result

set @sourceTable = 'gen_048_'+@result
select @sourceTable

declare @string2 varchar(255)
set @string2 = (select convert(varchar(55),refdate)+''-''+convert(varchar(55),refcount) FROM @sourceTable)
select @string2

This is the error

Must declare the table variable "@sourceTable".

7
  • What is the value of @test? And what do you get now? What did you expect to get instead? Commented Mar 28, 2016 at 7:24
  • First mention what's wrong with your current code Commented Mar 28, 2016 at 7:24
  • Need Dynamic Query Commented Mar 28, 2016 at 7:25
  • 1
    you have to declare the variable @sourceTable. Commented Mar 28, 2016 at 7:29
  • 1
    Combine sagi's and prdp's answers to solve your problem. Commented Mar 28, 2016 at 7:42

3 Answers 3

2

You need dynamic query

SET @string2 = 'select convert(varchar(55),refdate)+''-''+convert(varchar(55),refcount) FROM '
               + Quotename(@sourceTable)

EXEC (@string2) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks~! this one works just fine. my only problem now is how to update the rows of this reference number.
1

You are not declaring @sourcetable , the error shouts it loud and clear.

Add this At the beginning :

declare @sourcetable varchar(50)

Also, I believe you need to use dynamic SQL for this sort of queries and variable using.

1 Comment

Yes I declared the @sourceTable that is why its kinda weird that I still got that error. I just forgot to put it there.
0
set @string2 = (select convert(varchar(55),refdate)+'-'+convert(varchar(55),refcount) FROM @sourceTable)

4 Comments

I already did this one, this is the exact string from the other question but its not working. This is the error Must declare the table variable "@sourceTable".
obviously from the code you provided, don't have such variable : @sourceTable
it seem "@sourceTable" is varchar? so you add one more line something like this. ==> declare "@sourceTable" varchar(15)
ok then, this line "set @string2 = (select convert(varchar(55),refdate)+''-''+convert(varchar(55),refcount) FROM @sourceTable)" you use "@sourceTable" as a table? what is the column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.