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)
 Stored Proc with sp_executesql and complex parms?

Author  Topic 

misscrf
Starting Member

10 Posts

Posted - 2010-10-18 : 17:19:06
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!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-18 : 17:31:10
You do not need dynamic SQL for what you have posted. Can you explain why you think this needs to be dynamic (sp_executesql)?

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

Subscribe to my blog
Go to Top of Page

misscrf
Starting Member

10 Posts

Posted - 2010-10-19 : 09:25:19
Thanks for responding! The above is just 1 of about 7 queries that I am trying to run all at once. In each of them, I need to put a parameter. It needs to be an IN parameter (comma delimited or and OR list of values). Each possible parameter needs to match the left or begins with of a field value in one of the tables that is used in every query. Whatever the parameter or parameters is that are supplied to the query, it/they will be the same parameter for all queries. I have been tasked with taking all of these queries and making them into a stored procedure, so that we can supply 1 ore more parameters and have all queries run. Some day, over the rainbow, I will be going to an SSRS class where I will learn to put this all into a nifty report! I can't wait for that.

I will try re-creating this without the sp_executesql, but can you please shed some light on why my parm is not working? If you know?

Thanks!

Thanks!
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-19 : 09:29:28
May be because of

set @sessions ='ABC123456_000001' --16 Chars
In your query you are selecting 15 chars
left(e.title,15)
Go to Top of Page

misscrf
Starting Member

10 Posts

Posted - 2010-10-19 : 16:00:39
you are exactly right. I fixed that. It is now all set without the dynamic sql my proc became the following:

create procedure sp_Session_Summary @sessions nvarchar(max) as
Select e.title as Session,
count(d.id) as SessionDocuments,

case when sum(d.filesize) is null
then '0' else sum(d.filesize)
end as SessionSizeBytes,

case when cast(sum(d.filesize)/1024.0/1024.0/1024.0 as decimal(10,5))is null
then '0' else cast(sum(d.filesize)/1024.0/1024.0/1024.0 as decimal(10,5))
end as SessionSizeGB

from tbldoc d
right join tbledsessions e on e.id = d.edsessionid
where left(e.title,16) In (select item from fnSplit(@sessions, '|'))
group by e.title



Thanks for getting me away from the dynamic sql since it wasn't needed!

Thanks!
Go to Top of Page
   

- Advertisement -