This is a discussion on request issue within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I've got two primary keys in a table: Constraint(QueryId, ConstraintName) In a stored procedure I select {QueryId, ConstraintName} ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi, I've got two primary keys in a table: Constraint(QueryId, ConstraintName) In a stored procedure I select {QueryId, ConstraintName} couples that match some criteria, and what I want to do is specifying in my a SELECT statement that I want all of the {QueryId, ConstraintName} that are not in my stored procedure result. With only one field, it would be easy : Select * from Constraint where QueryId not in (Select QueryId from OtherTable) My explanations are not great but I think it's enough to understand what I want. Select * from Constraint where QueryId and ConstraintName not in (select QueryId ,ConstraintName from OtherTable) --> of course not correct, but then how can I do that ? Thx |
| |||
| I've tried this, but it doesn't work. CREATE PROCEDURE pr_Admin_GetConstraintMessages AS SELECT CM.QueryId, Message, Type, Q.QueryName, Q.RootTable, ConstraintName FROM ConstraintMessages CM JOIN Queries Q ON CM.QueryId = Q.QueryId WHERE (CM.QueryId, ConstraintName) NOT IN (SELECT QueryId, ConstraintName from fn_Admin_GetOrphanedMessages) GO fn_Admin_GetOrphanedMessages returns (queryid, constraintName) couples. Error message : Incorrect syntax near ',' I guess it is my WHERE statement... |
| |||
| I've tried this, but it doesn't work. CREATE PROCEDURE pr_Admin_GetConstraintMessages AS SELECT CM.QueryId, Message, Type, Q.QueryName, Q.RootTable, ConstraintName FROM ConstraintMessages CM JOIN Queries Q ON CM.QueryId = Q.QueryId WHERE (CM.QueryId, ConstraintName) NOT IN (SELECT QueryId, ConstraintName from fn_Admin_GetOrphanedMessages) GO fn_Admin_GetOrphanedMessages returns (queryid, constraintName) couples. Error message : Incorrect syntax near ',' I guess it is my WHERE statement... |
| |||
| I've tried this, but it doesn't work. CREATE PROCEDURE pr_Admin_GetConstraintMessages AS SELECT CM.QueryId, Message, Type, Q.QueryName, Q.RootTable, ConstraintName FROM ConstraintMessages CM JOIN Queries Q ON CM.QueryId = Q.QueryId WHERE (CM.QueryId, ConstraintName) NOT IN (SELECT QueryId, ConstraintName from fn_Admin_GetOrphanedMessages) GO fn_Admin_GetOrphanedMessages returns (queryid, constraintName) couples. Error message : Incorrect syntax near ',' I guess it is my WHERE statement... |
| |||
| |
| |||
| Use NOT EXISTS rather than NOT IN: SELECT * FROM [Constraint] AS T WHERE NOT EXISTS (SELECT * FROM OtherTable WHERE queryid = T.queryid AND constraintname = T.constraintname) CONSTRAINT is a reserved word and therefore not a good choice for a table name. -- David Portas SQL Server MVP -- |