Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 can we use variables in insert statement

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-01-08 : 08:08:33
amrith writes "declare @name varchar(50)
set @name = journal
insert @name values ('2/2/01',cash,100,100)




Here journal is the name of table and we have stored it in a variable and used in the insert statement. Will it be successful?if not, please send what we have to do."

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-01-08 : 08:18:45
It will not work as it is. You need to use Dynamic SQL to make it work.

Declare @sql varchar(1000)

set @sql = 'insert ' + @name + ' values (''2/2/01'',cash,100,100)'
Exec(@sql)



But why do you want table name to be dynamic?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-08 : 08:36:49
Cash variable must be prefixed with @.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -