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.
| Author |
Topic |
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2011-06-03 : 09:50:36
|
| Hi, I want to check 3 tables for a titleSo we got:table 1idtitledescriptiontable 2idtitledescriptiontable 3idtitledescriptionThe sp should return true if the @title matches a record in one of the three tables.How would I do that?The secret to creativity is knowing how to hide your sources. (Einstein) |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2011-06-03 : 09:55:43
|
| Right now, I've got: @Title nvarchar(255)ASselect case when EXISTS( select * from dbo.Table1 where LOWER(Title)=LOWER(@Title)) then 1 else 0 end but it should be extended to 2 more tables.The secret to creativity is knowing how to hide your sources. (Einstein) |
 |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2011-06-06 : 06:53:38
|
| No-one's got a suggestion? |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-06-06 : 08:11:29
|
Your approach of using "exists" is the best approach in this case. To extend it to other tables, I would use the low-tech approach such as this:@Title nvarchar(255),@found int = null OUTPUTASset @found = 0;if exists (select * from dbo.Table1 where LOWER(Title)=LOWER(@Title)) set @found = 1;if @found = 1 return;if exists (select * from dbo.Table2 where LOWER(Title)=LOWER(@Title)) set @found = 1;if @found = 1 return;if exists (select * from dbo.Table3 where LOWER(Title)=LOWER(@Title)) set @found = 1;return; There may be some other slight efficiencies that can be found, for example, if your collation is known to be case-insensitive, you may not need to convert to lower. Other than that, this is probably the most efficient. |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2011-06-06 : 13:33:36
|
| Normalized tables are your FRIENDS!http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-06-06 : 14:09:38
|
| couldn't agree more with Don! if your database does not have normalized well thought out tables and relationships you will be doing cirque du soleil code and sprocs.it's like trying to use gum to patch up a leaking submarine. yes it will work for a wee bitIf you don't have the passion to help people, you have no passion |
 |
|
|
|
|
|