December 6, 2008 at 1:12 am
Hello, I'm hoping someone has come across a similar issue and may have a solution for me.
Currently have the following table setup (only showing affected columns). Column list is as follows:
Table Name: tblActivity (tblDenyActivity)
DenyDateUserID
DenyDate
InvalidDate
Table Name: tblUserData
UserID
Table Name: tblUserSkillset
UserID
SkillsetTypeID
Table Name: tblSkillsetType
SkillsetTypeID
SkillSetType
SQLSnippet
SELECT tblDenyActivity.DenyDateUserID, tblDenyActivity.DenyDate FROM dbo.tblActivity AS tblDenyActivity INNER JOIN
dbo.tblUserData ON tblDenyActivity.DenyDateUserID = dbo.tblUserData.UserID AND tblDenyActivity.InvalidDate IS NULL INNER JOIN dbo.tblUserSkillset ON dbo.tblUserData.UserID = dbo.tblUserSkillset.UserID INNER JOIN dbo.tblSkillsetType ON dbo.tblUserSkillset.SkillsetTypeID = dbo.tblSkillsetType.SkillsetTypeID
WHERE (dbo.tblSkillsetType.SQLSnippet)
The column SQLSnippet will contain something like the following (DenyDate IS NOT NULL AND DATEDIFF(dd,DenyDate,GETDATE()) > 31)
So my end result would be:
SELECT tblDenyActivity.DenyDateUserID, tblDenyActivity.DenyDate FROM dbo.tblActivity AS tblDenyActivity INNER JOIN
dbo.tblUserData ON tblDenyActivity.DenyDateUserID = dbo.tblUserData.UserID AND tblDenyActivity.InvalidDate IS NULL INNER JOIN dbo.tblUserSkillset ON dbo.tblUserData.UserID = dbo.tblUserSkillset.UserID INNER JOIN dbo.tblSkillsetType ON dbo.tblUserSkillset.SkillsetTypeID = dbo.tblSkillsetType.SkillsetTypeID
WHERE (DenyDate IS NOT NULL AND DATEDIFF(dd,DenyDate,GETDATE()) > 31)
Has anyone ever tried to do anything like this?
December 6, 2008 at 1:57 am
Double posted. Answer here: http://www.sqlservercentral.com/Forums/Topic615077-8-1.aspx#bm615084
December 6, 2008 at 2:13 am
Sorry about that, meant to post in the SQL Server 2005 section first. The delete button didn't work either when I tried to delete the post in the SQL 2000,7 section..
The post you link to doesn't really help me out.
The above query is already a part of a much larger dynamic SQL query, it will be a derived table.
I would think WHERE (dbo.tblSkillsetType.SQLSnippet) would cause an error, going to try it out and see what happens.
December 6, 2008 at 2:26 am
Derek (12/6/2008)
I would think WHERE (dbo.tblSkillsetType.SQLSnippet) would cause an error
Yes it will. The where clause must consist of conditions, where one expression is compared against another. As I said in my reply to your other post, the only way you're going to do this is Dynamic SQL.
Be aware of the downsides of dynamic SQL and watch for SQL injection.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 6, 2008 at 2:44 am
I'm trying to avoid having to use a loop, your suggestion would require a loop through each skill set type. Trying to keep it as one query.
Could it be possible in the FROM?
SELECT tblDenyActivity.DenyDateUserID, tblDenyActivity.DenyDate FROM dbo.tblActivity AS tblDenyActivity INNER JOIN
dbo.tblUserData ON tblDenyActivity.DenyDateUserID = dbo.tblUserData.UserID AND tblDenyActivity.InvalidDate IS NULL AND (SELECT dbo.tblSkillsetType.SQLSnippet FROM dbo.tblSkillsetType WHERE dbo.tblSkillsetType.SkillsetTypeID = tblExpectations.SkillsetTypeID) INNER JOIN dbo.tblUserSkillset ON dbo.tblUserData.UserID = dbo.tblUserSkillset.UserID INNER JOIN dbo.tblSkillsetType ON dbo.tblUserSkillset.SkillsetTypeID = dbo.tblSkillsetType.SkillsetTypeID
tblExpectations.SkillsetTypeID is from the larger dynamic query.
No worries on the SQL injection, string is parsed by the application before being sent to the sp.
December 6, 2008 at 3:05 am
Derek (12/6/2008)
Could it be possible in the FROM?
No. Same problem as in the where. Join conditions must be conditions, not statements.
Just trying to parse that query shows that it won't work.
Msg 4145, Level 15, State 1, Line 2
An expression of non-boolean type specified in a context where a condition is expected, near 'INNER'.
The only way to include a condition that's stored as a string is to use dynamic SQL.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 6, 2008 at 4:49 am
You might want to pick up a copy of Itzik Ben-Gan's Inside SQL Server TSQL Querying. You're missing out on some core logical constructs as to how TSQL works and how SQL Server works with it. Read through the first two chapters on query processining. They lay out the methods used by the optimizer and query engine such that you'll see better how to deal with this construction issue that you're hitting.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply