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)
 Problem with dynamic sql statement (

Author  Topic 

dmorand
Starting Member

20 Posts

Posted - 2009-01-13 : 09:38:01
I'm having some trouble with a dynamic query I wrote. It works fine when I run it as a standard query.

I'm getting the following error when I call the stored procedure:
"Syntax error converting character string to smalldatetime data type."

I know it's related to the lines with the @pddatetime variables because I commented them out and re-ran it and the sp returns data.

Here is the related code snippet:

DECLARE @pddatetime smalldatetime,
@sqlStatement varchar(2000) --Dynamic Query
SET @pddatetime=GETDATE()

---- this query will return the user and their user group
SELECT @sqlStatement = 'select distinct t1.loginid,
t1.username, t1.userdescription, t4.groupname,t4.groupdescription
from hsuser t1 with (nolock)
join hsu2uglink t2 with (nolock)
on t1.objectid = t2.user_oid
join huserassignment t3 with (nolock)
on t2.group_oid = t3.usergroup_oid
join hsusergroup t4 with (nolock)
on t2.group_oid=t4.objectid
where t1.endtime >= ' + @pddatetime
' and t1.begintime <= ' + @pddatetime +
' and t3.endtime >= ' + @pddatetime +
' and t3.begintime <= ' +@pddatetime +
' and t4.endtime >= ' +@pddatetime +
' and t4.begintime <= ' +@pddatetime +
' order by groupname'

PRINT(@sqlStatement)

Ifor
Aged Yak Warrior

700 Posts

Posted - 2009-01-13 : 09:58:43
' and t1.begintime <= ''' + CAST(@pddatetime AS varchar(50))+ ''''

etc

ps Any reason why you want this as a dynamic query?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-13 : 10:19:04
as per posted query, there's no reason for you to use dynamic sql.
Go to Top of Page

dmorand
Starting Member

20 Posts

Posted - 2009-01-13 : 11:36:21
thanks for the help! I setup a casted variable ahead of time and used that rather than keep having to cast the value over and over in the dynamic query.

I have it setup as a dynamic query right now because I'm going to be adding to the query, I just wanted to get the base version of it working first.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-13 : 11:46:09
quote:
Originally posted by dmorand

thanks for the help! I setup a casted variable ahead of time and used that rather than keep having to cast the value over and over in the dynamic query.

I have it setup as a dynamic query right now because I'm going to be adding to the query, I just wanted to get the base version of it working first.


add to code what? conditional filters? even then you dont require dynamic sql
Go to Top of Page

dmorand
Starting Member

20 Posts

Posted - 2009-01-14 : 08:55:26
So it I want to add different criteria into my where clause I don't need dynamic sql for this? There will only be extra where clauses based on what parameters are sent. If there aren't any sent, then I won't have a where clause at all.

Here's what my SP looks like now:


ALTER PROCEDURE [HA_Security]
@report_type varchar(50), --User,Usergroup,Role
@user_list varchar(500), --List of Users
@usergroup_list varchar(500), --List of Usergroups
@role_list varchar(500) --List of Roles
AS
BEGIN
DECLARE @pddatetime smalldatetime,
@convdatetime varchar(50),
@sqlStatement varchar(2000) --Dynamic Query
SET @pddatetime = GETDATE()
SET @convdatetime = convert(varchar(50),@pddatetime,101)

PRINT(@pddatetime)
PRINT(@convdatetime)
IF @report_type = 'UserGroup'
BEGIN
PRINT(@report_type)
---- this query will return the user and their user group
SELECT @sqlStatement = 'select distinct t1.loginid,
t1.username, t1.userdescription, t4.groupname,t4.groupdescription
from hsuser t1 with (nolock)
join hsu2uglink t2 with (nolock)
on t1.objectid = t2.user_oid
join huserassignment t3 with (nolock)
on t2.group_oid = t3.usergroup_oid
join hsusergroup t4 with (nolock)
on t2.group_oid=t4.objectid
where t1.endtime >= ' + @convdatetime
' and t1.begintime <= ' + @convdatetime +
' and t3.endtime >= ' + @convdatetime +
' and t3.begintime <= ' @convdatetime +
' and t4.endtime >= ' + @convdatetime +
' and t4.begintime <= ' + @convdatetime
IF Len(@usergroup_list) > 0
BEGIN
SELECT @sqlStatement = @sqlStatement +
' and loginid IN(' + @user_list + ')'
END
SELECT @sqlStatement = @sqlStatement +
' order by groupname'
PRINT(@sqlStatement)

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 09:19:02
yup. you need only this

ALTER PROCEDURE [HA_Security]
@report_type varchar(50), --User,Usergroup,Role
@user_list varchar(500), --List of Users
@usergroup_list varchar(500), --List of Usergroups
@role_list varchar(500) --List of Roles
AS
BEGIN
DECLARE @pddatetime smalldatetime,
@convdatetime varchar(50),
@sqlStatement varchar(2000) --Dynamic Query
SET @pddatetime = GETDATE()
SET @convdatetime = convert(varchar(50),@pddatetime,101)


IF @report_type = 'UserGroup'
BEGIN

---- this query will return the user and their user group
select distinct t1.loginid,
t1.username, t1.userdescription, t4.groupname,t4.groupdescription
from hsuser t1 with (nolock)
join hsu2uglink t2 with (nolock)
on t1.objectid = t2.user_oid
join huserassignment t3 with (nolock)
on t2.group_oid = t3.usergroup_oid
join hsusergroup t4 with (nolock)
on t2.group_oid=t4.objectid
where t1.endtime >= @convdatetime
and t1.begintime <= @convdatetime
and t3.endtime >= @convdatetime
and t3.begintime <= @convdatetime
and t4.endtime >= @convdatetime
and t4.begintime <= @convdatetime
and (Len(@usergroup_list) = 0
or ',' + @user_list + ',' LIKE '%,'+cast(loginid as varchar(20))+ ',%'
)
order by groupname
END

also why are you converting date to varchar? i dont think that is reqd. Also why IF condition? you dont have any other branches
Go to Top of Page

dmorand
Starting Member

20 Posts

Posted - 2009-01-14 : 09:45:43
I was converting the date to varchar because that's what I had to do to get the date field to work with the dynamic sql statement, but now it looks like I don't need to use dynamic sql at all.

Thanks for that logic, but I'm a little confused as to how this even works.

It's adding a comma before and after the list, and then using like to compare against the list. O nevermind I understand how it works now as I started really reading the logic.

Thanks a bunch, it seems like I've been using all of this dynamic sql for nothing.



Go to Top of Page

dmorand
Starting Member

20 Posts

Posted - 2009-01-14 : 09:47:10
Also, why couldn't I use the IF statement? I am a programmer so I'm used to using IF statements and loops. I have to say that SQL programming has been the hardest conceptually to understand.
Go to Top of Page

dmorand
Starting Member

20 Posts

Posted - 2009-01-14 : 09:53:45
When would it be ideal to use dynamic sql? I'm trying to think of a situation where I'd use it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 10:06:44
quote:
Originally posted by dmorand

Also, why couldn't I use the IF statement? I am a programmer so I'm used to using IF statements and loops. I have to say that SQL programming has been the hardest conceptually to understand.


nope. i was not telling its a bad practise. i was telling its not relevant here as you had only single select batch
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 10:08:06
quote:
Originally posted by dmorand

When would it be ideal to use dynamic sql? I'm trying to think of a situation where I'd use it.


consider scenario where you objects might change dynamically or when you cant determine the columns required in resultset, like in case of cross tab. here you can use dynamic sql
Go to Top of Page

dmorand
Starting Member

20 Posts

Posted - 2009-01-14 : 11:06:00
quote:
Originally posted by visakh16

quote:
Originally posted by dmorand

When would it be ideal to use dynamic sql? I'm trying to think of a situation where I'd use it.


consider scenario where you objects might change dynamically or when you cant determine the columns required in resultset, like in case of cross tab. here you can use dynamic sql




Ok that makes sense. I kind of figured I was doing something wrong by using dynamic sql all the time, it was just making my life harder. Thanks for all your helpful advice, I truly appreciate it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 11:59:56
you're welcome
Go to Top of Page
   

- Advertisement -