December 6, 2010 at 2:33 am
2 Posts
Posted - 12/06/2010 : 03:42:33 Show Profile Email Poster Edit Topic Reply with Quote
Department_ID nvarchar(2)
Possition_ID nvarchar(2)
give me code to solve : with each Department_ID
have only <=1 Department manager (Possition_ID='00')
thanks
December 6, 2010 at 2:39 am
Please post table definitions, sample data and expected results as per http://www.sqlservercentral.com/articles/Best+Practices/61537/
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, 2010 at 8:10 am
Please also try to solve this yourself. We are more than willing to help, but we are not here to do your work, or homework.
December 6, 2010 at 10:27 am
First, can you verify that this is NOT a homework / test / interview question?
Instead of doing this through a trigger, have you tried a filtered indexed view with either a unique index or primary key on the column?
Something like this for the view:
SELECT (list of columns from base table)
FROM (whatever the base table is)
WHERE Possition_ID = '00';
Note that to create an index on a view, you first need to materialize it by adding a clustered index. The following might be a good candidate for that, but if not you will need another clustered index.
Then create a unique or PK constraint on Department_ID + Possition_ID.
In SQL 2008, you could just create a unique, filtered index on Department_ID + Possition_ID.
This is usually more efficient than trying to maintain through a trigger.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
December 6, 2010 at 6:13 pm
I want: on my table (Department_ID nvarchar(2),Possition_ID nvarchar(2))
never occur: exists
SELECT Department_ID FROM MyTable
WHERE Possition_ID = '00'
GROUP BY Department_ID HAVING COUNT(Possition_ID) >= 2
Help me
December 7, 2010 at 8:12 am
This
letanduc (12/6/2010)
I want: on my table (Department_ID nvarchar(2),Possition_ID nvarchar(2))never occur: exists
Makes no sense. What is "never occur: exists?"
Please spend more than 10 sec and write something that describes the behavior you are looking for.
December 10, 2010 at 3:55 am
As stated by the others: happy to help not do your job for you/get you a job based on answering SQL questions for you.
Please clearly state what you are trying to achieve.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply