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 2005 Forums
 Transact-SQL (2005)
 dynamic inserting of records

Author  Topic 

karthikeyan.marlen
Starting Member

16 Posts

Posted - 2011-09-21 : 06:18:59
hi

i tried a task of inserting records to a table dynamically using stored procedure

as follows

create procedure proc2 (@tabl varchar(20),@sno int,@name varchar(30),@age int)
as
begin
declare @qry varchar(max);
set @qry='insert into ' + @tabl + ' values (sno,name,age) ( '+@sno+', '''+ @name +''','+ @age +')';
exec (@qry);
end

but it shows error as 'Msg 245, Level 16, State 1, Procedure proc2, Line 5
Conversion failed when converting the varchar value 'insert into demo values (sno,name,age) ( ' to data type int.'

so please clear me regarding this

Thanks

karthikeyan

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-21 : 06:23:40
[code]
create procedure proc2 (@tabl varchar(20),@sno int,@name varchar(30),@age int)
as
begin
declare @qry varchar(max);
set @qry='insert into ' + @tabl + ' values (sno,name,age) ( '+ cast(@sno as varchar(20))+', '''+ @name +''','+ cast(@age as varchar(5)) +')';
exec (@qry);
end


[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -