SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Dynamic Update Query Problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

prashantdighe
Starting Member

21 Posts

Posted - 05/07/2012 :  01:25:01  Show Profile  Reply with Quote
Hello Friends,
I m confused did i do correct or not???


set @query1 ='update #temp set ['+@col1+']='+@a+' where id='+CAST(@COUNT as varchar)

EXEC @query1

giving error like

Msg 2812, Level 16, State 62, Procedure Test1, Line 64
Could not find stored procedure ''.

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/07/2012 :  01:30:27  Show Profile  Reply with Quote

EXEC (@query1)



KH
Time is always against us

Go to Top of Page

prashantdighe
Starting Member

21 Posts

Posted - 05/07/2012 :  01:36:58  Show Profile  Reply with Quote
Thank u khtan,

I tried,

set @query1 ='update #temp set ['+@col1+']='+@a+' where id=' +CAST(@COUNT as varchar)
exec sp_executesql @query1

because,

In @col1 the value of date(e.g.12/03/2012) as column name ,@a having value 'A' as string and @COUNT has value of id means 1,2,3

Now problem when I do above, the value of @a which is 'A' set as column name and give following error...

Msg 207, Level 16, State 1, Line 1

Invalid column name 'A'.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/07/2012 :  01:45:59  Show Profile  Reply with Quote
if @a is string you need to enclosed it in quote in your dynamic sql

set @query1 ='update #temp set ['+@col1+']='''+@a+''' where id=' +CAST(@COUNT as varchar) 



KH
Time is always against us

Go to Top of Page

prashantdighe
Starting Member

21 Posts

Posted - 05/07/2012 :  01:55:49  Show Profile  Reply with Quote
Wow Thank u khtan its done and working fine Thank you again for your fast and accuarate reply!!!!!!!!!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000