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
 How to use select statement inside insert statemen

Author  Topic 

Sumathy
Starting Member

2 Posts

Posted - 2014-10-20 : 03:17:26
In the below code i want to use select statement for getting customer address1,customeraddress2,customerphone,customercity,customerstate,customercountry,customerfirstname,customerlastname from customer table.Rest of the things will be as it is in the following code.How do i do this?

INSERT INTO EMImportListing ("
sql += " CustId,Title,Description,JobCity,JobState,JobPostalCode,JobCountry,URL,Requirements, "
sql += " IsDraft,IsFeatured,IsApproved,"
sql += " Email,OrgName,customerAddress1,customerAddress2,customerCity,customerState,customerPostalCode,customerCountry,customerFirstname,customerLastName,customerPhone,"
sql += " HideContactInfo, HideOrgName, HideName, HidePhone, HideEmail, "
sql += " ImportReferenceNumber, ImportCategory,ImportSource,FlgEmlisting "
sql += " ) VALUES ( "
sql += " @CustId, @Title, @Description, @JobCity, @JobState, @JobPostalCode, @JobCountry, @URL, @Requirements, "
sql += " 'Y','N','Y', "
sql += " @Email,@OrgName,"
sql += " @HideContactInfo, @HideOrgName, @HideName, @HidePhone, 'Y', "
sql += " @ImportReferenceNumber, @ImportCategory,'Bull','N'"
sql += " ) "

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-10-20 : 11:56:18
Just switch your values to the select statement you want to use.

But you need to update your code as it's vulnerable to SQL injection. Never concatenate a string together to form your SQL command. Use parameterized queries instead. Be sure you read up on this topic to avoid SQL injection.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -