Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Use Table Column Contents As Where Clause Expand / Collapse
Author
Message
Posted Saturday, December 6, 2008 1:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 15, 2010 1:42 PM
Points: 4, Visits: 8
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?
Post #615081
Posted Saturday, December 6, 2008 1:57 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:50 PM
Points: 20,730, Visits: 32,490
Double posted. Answer here: http://www.sqlservercentral.com/Forums/Topic615077-8-1.aspx#bm615084


Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #615086
Posted Saturday, December 6, 2008 2:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 15, 2010 1:42 PM
Points: 4, Visits: 8
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.
Post #615092
Posted Saturday, December 6, 2008 2:26 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:04 AM
Points: 40,172, Visits: 36,559
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 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #615093
Posted Saturday, December 6, 2008 2:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 15, 2010 1:42 PM
Points: 4, Visits: 8
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.
Post #615095
Posted Saturday, December 6, 2008 3:05 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:04 AM
Points: 40,172, Visits: 36,559
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 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #615099
Posted Saturday, December 6, 2008 4:49 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 13,867, Visits: 28,265
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
The Scary DBA
Author of: SQL Server Query Performance Tuning
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #615118
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse