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)
 Help understanding this

Author  Topic 

noex318
Starting Member

1 Post

Posted - 2011-05-26 : 17:41:01
I'm trying to understand the code below. I have a few questions, the big one is how is Enterdate being used as comparison in it's own SET statement? Also the use of case statements is very difficult to understand in this, is there an easier way to display it? From what I understand, this is updating a date field in tblMyWorksUserInfo, but it's very cryptic to me on how it's being set.

declare @Submissionid VARCHAR(10)
declare @postingdate VARCHAR(100)
declare @sql varchar(MAX)

set @Submissionid = 11111

set @postingdate = '(SELECT PostingDate FROM tblSubmissionInfo WHERE (Submissionid IN ('+ @Submissionid +')))'

set @sql = 'UPDATE tblMyWorksUserInfo SET '
set @sql = @sql + 'EnterDate = case when (Enterdate+getdate()- '+@postingdate+' >getdate()) or (Enterdate+getdate()- '+@postingdate+' < '+@postingdate+' ) then '
set @sql = @sql + 'case when getdate()-enterdate+ '+@postingdate+' >=enterdate and enterdate >= '+@postingdate+' then enterdate '
set @sql = @sql + 'when enterdate< '+@postingdate+' then '+@postingdate+' '
set @sql = @sql + 'else getdate()-enterdate+ '+@postingdate+' end '
set @sql = @sql + 'else Enterdate+getdate()- '+@postingdate+' end '
set @sql = @sql + 'WHERE (UserID IN (SELECT User_id FROM tblSubmissionUserTracker WHERE (Submission_id IN (' + @Submissionid + '))));'
exec (@sql)

nathans
Aged Yak Warrior

938 Posts

Posted - 2011-05-26 : 18:32:33
This script is constructing a dynamic sql statement that is executed at the end via exec(@sql).

To see the statement that is actually run change your execute statement to
print @sql
instead of
exec(@sql)
Go to Top of Page
   

- Advertisement -