SQLTeam.com | Weblogs | Forums

How to use select query from string in insert into table query


#1

I have a @sql string, which has complete select query, how to use it with the below insert table query:

what is the proper way to use @SQL with below query; thank you very much for the helpful info.

	CREATE TABLE #tmpRM
	(ID int identity, rmid int, ProgID int, ProjID int, ContractID int, Departamento varchar(50),
		Proceso varchar(50), contractno varchar(50),rmtitle varchar(100), ContractorRefNo varchar(50),sequenceno decimal(10,2), barcode varchar(50),
		destdepartment varchar(50), origsender varchar(50), documentdate datetime, ReceivedDate datetime, rmloggeddate date, acknowledgeddate datetime, 
		documenttypeid int, LogTypeID int, documentsourceid int, SubjectID int, origorganization varchar(50))
		insert into #tmpRM(rmid , ProgID , ProjID , ContractID,Departamento ,
		Proceso , contractno , rmtitle,ContractorRefNo ,sequenceno , barcode ,
		destdepartment , origsender , documentdate , ReceivedDate , rmloggeddate , acknowledgeddate , 
		documenttypeid , LogTypeID , documentsourceid , SubjectID , origorganization )

		 (
		 '' + @SQL + ''
		 )

#2

Make the code that starts with 'insert into..." into a string and prepend it to @SQL. Then use sp_executesql using that string.