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
 General SQL Server Forums
 New to SQL Server Programming
 insert command in table inside procedure

Author  Topic 

subhaoviya
Posting Yak Master

135 Posts

Posted - 2009-07-27 : 23:13:23
am trying to pass the table name into store procedure, when i use inside the procedure for inserting record. it trough's error.

create procedure sampleproc
@tablename varchar(25)
as
insert into @tablename values('name','address')




how to solve it?

thanks
by
subha

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-27 : 23:23:44
why do you want to do this ?

The only way to solve is via the Dynamic SQL way.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

subhaoviya
Posting Yak Master

135 Posts

Posted - 2009-07-27 : 23:28:14
i want to update records from single table to many table based on the name and place.

i thought that if common procedure accepts the table name. the process of check and inserting all will done by calling it repeatedly.
that's why.
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-07-28 : 02:33:50
quote:
Originally posted by subhaoviya

i want to update records from single table to many table based on the name and place.

i thought that if common procedure accepts the table name. the process of check and inserting all will done by calling it repeatedly.
that's why.



Hi Subetha,

Your requirement is clear you can dynamical handle the Table, So u must use Dynamic SQL as khtan says.

Its simple,

Declare @sql varchar(8000)

@sql='insert into '+ @tablename +'" values('name','address')"'

Exec(@sql)

Here The query is build Dynamically, and get executed


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-28 : 03:10:12
quote:
Originally posted by senthil_nagore

quote:
Originally posted by subhaoviya

i want to update records from single table to many table based on the name and place.

i thought that if common procedure accepts the table name. the process of check and inserting all will done by calling it repeatedly.
that's why.



Hi Subetha,

Your requirement is clear you can dynamical handle the Table, So u must use Dynamic SQL as khtan says.

Its simple,

Declare @sql varchar(8000)

@sql='insert into '+ @tablename +'" values('name','address')"'

Exec(@sql)

Here The query is build Dynamically, and get executed


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/



It should be

set @sql='insert into '+ @tablename +' values(''name'',''address'')'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-28 : 03:15:47
nobody posted this link yet ?

A must read before using Dynamic SQL
The Curse and Blessings of Dynamic SQL



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -