I have a query that is set up like this:declare @sqlstr nvarchar(max)declare @sessions nvarchar(max)set @sessions ='ABC123456_000001'set @sqlstr='select e.id, e.title, e.created, e.itemcount, e.itemtotal, e.recordswritten, e.errors, e.excluded, e.partiallyexcluded, e.incomplete, e.size as SizeBytes, e.size/1024/1024/1024 as GBSize, count(d.id) as DocCount'set @sqlstr=@sqlstr + ' ' + 'from tbldoc d right join tbledsessions e on e.id = d.edsessionid'set @sqlstr=@sqlstr + ' ' + 'where left(e.title,15) In (' + @sessions + ')'set @sqlstr=@sqlstr + ' ' + 'group by e.id, e.title, e.created, e.itemcount, e.itemtotal, e.recordswritten, e.errors, e.excluded, e.partiallyexcluded, e.incomplete, e.size'execute sp_executesql @sqlstr
Issue 1:You will notice that the @sessions in the 2nd set statement is linking to the left 15 characters of a table column. What is more, I need to be able to have this parameter allow for a user to enter a comma delimited list of these strings that will all be 15 characters long.I am not sure how to create an in criteria to compare the left string of a column. Right now when I run this, I get error: Invalid column name 'ABC123456_000001'. Issue 2:I have a series of these queries, all displaying different info, but with the same parms. I need to make it into 1 big stored proc. I am not sure how to make this a stored proc with the sp_executesql in the proc. Is that allowed?I am not necessarily a newb, but I am probably novice to intermediate at SQL. Definitely novice at stored procs and what they can do.Any help is appreciated!!Thanks!