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)
 insert into statement

Author  Topic 

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-12-15 : 16:10:15
I have an insert statement and the values are coming from different sources as follows:
From a column in another table, from @Parameter or a fixed assignment. Its illustrated in the example. Any suggestions as to How I can do this?


insert into table1 (col1_identity, col2, col3, col4, col5)
select(@id, null, @col3, a.productName, b.productType)
from [NOTHING ????????????????????] -- Please note I don't have a from table. THats the issue!!!!
inner join table2 a on a.col4=@col4
inner join table3 b on b.col5=@col5

Thanks,

SA

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-15 : 16:13:21
insert into table1 (col1_identity, col2, col3, col4, col5)
select @id, null, @col3, a.productName, b.productType
from table2 a on a.col4=@col4
join table3 b on b.col5=@col5

Are @id, @col3, @col4, and @col5 parameters or columns in one of the tables? It is very unclear.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-12-15 : 16:14:53
Thanks tkizer.

These are parameters!

SA
Go to Top of Page

goodsolution
Starting Member

38 Posts

Posted - 2009-12-15 : 16:20:18
You have to write something like this

decalre @col3 varchar(10)
set @col3 = (select statement)
decalre @col5 varchar(10)
set @col5 = (select statement)

EXEC ('insert into table1 (col2, col3, col4, col5)
select '''', '+@col3+', t1.productname, b.prodcutype from table1 T1 inner join
table2 T2 on t1.col4 = '+@col4+'
innert join table3 T3 on
t3.col5 = '+@col5+'')



-Thanks
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-12-15 : 16:34:46
select '''' ==> a null value with the surrounding open and close apostrophe. Did I get that Right?

SA
Go to Top of Page

goodsolution
Starting Member

38 Posts

Posted - 2009-12-15 : 16:36:37
Yes that is for null value. because we are writing EXEC ('') we need to keep '' ''.

-Thanks
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-12-15 : 16:45:51
This is the error msg (as indicated below). Obviously the line # are different. But if you can help me decipher the issue it would really help me.
I know I am close to the solution at this point.

Error Message:
Msg 102, Level 15, State 1, Procedure publicCompanyStoredProcedure, Line 5
Incorrect syntax near '@'.
Msg 137, Level 15, State 2, Procedure publicCompanyStoredProcedure, Line 19
Must declare the scalar variable "@col3".

SA
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-15 : 16:56:20
Please post the entire code of your procedure.
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-12-15 : 16:57:49
okay I finally figured out the error msg issue. Col2 which I have assigned to null is actually: FK, char(8),not null?

Please advice how to deal with it

SA
Go to Top of Page

goodsolution
Starting Member

38 Posts

Posted - 2009-12-15 : 17:51:13
so insert it with the right value instead of inserting wiht null,
if you are getting null from the parameter, you need to write ISNULL(@COL3, VALUE) function. if any value is OK, then use
ISNULL(@COL3, -1).



-Thanks
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-12-15 : 17:56:10
Thanks!

SA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-15 : 18:35:15
quote:
Originally posted by goodsolution

You have to write something like this

decalre @col3 varchar(10)
set @col3 = (select statement)
decalre @col5 varchar(10)
set @col5 = (select statement)

EXEC ('insert into table1 (col2, col3, col4, col5)
select '''', '+@col3+', t1.productname, b.prodcutype from table1 T1 inner join
table2 T2 on t1.col4 = '+@col4+'
innert join table3 T3 on
t3.col5 = '+@col5+'')



-Thanks



Do not use dynamic SQL for this. It is very unnecessary, a performance issue, and a security issue.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-12-15 : 19:41:12
Thanks tkizer. I followed your suggestions and it worked very well.

Thank you Very Much!!

It is something I had never used before hence the issue.

SA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-15 : 20:08:10
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -