SQL Server 2000: Assemble a query as a string

 

Print 'Step 2 Insert Accessions from xxxx and add where not there from CV already'
set @xxxxquery = '
INSERT INTO tbAccessionVerification(
UserName
,FY
,AccessionNumber
)

SELECT ''' + @username + '''
,''' + @FY + '''
,accessionnumber

FROM ' + @xxxxsourcequery + '
WHERE
-- NOT transferaccepteddate IS NULL
-- AND incomplete = 0
NOT accessionnumber COLLATE DATABASE_DEFAULT IN (
SELECT AccessionNumber from tbAccessionVerification
WHERE UserName = ''' + @username + ''' AND FY = ''' + @FY + '''
AND acType = ''AC'')'
print @xxxxquery
exec(@xxxxquery)