Solved

Help constructing Sql Statement/Stored Procedure

Posted on 2011-02-18
8
223 Views
Last Modified: 2012-05-11
I'm not sure if this is possible but here what I'd like to do.

I need to write a stored procedure that takes in a single value (a SKU number).   I have 8 tables in my database that all have a column called ItemSKU.  I'd like to check all eight tables in one procedure and see if I can find a match to that single SKU number.

Is there a way I can check each table for a match and, if a match is found, stop the search and return the columns (name, price, stock) that I want?
0
Comment
Question by:cdemott33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34928613
The columns name, price, stock should be returned from which table ?
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34928664
I am not sure whether you are looking for this
This stored procedure will search all the eight tables connected based on ItemSKU column and search in Table1 for the column [SKU number] for the value that is passed to the Stored Procedure.
If you want only the first matching record, use TOP, otherwise remove.
Hope you got some logic from this query
CREATE PROCEDURE usp_SearchSKUNumber
(
	@SKUNumber	INT
)
AS
BEGIN

	SELECT TOP 1 T1.name, T1.price, T1.stock 
	FROM TABLE1 T1
		INNER JOIN TABLE2 T2 ON T1.ItemSKU = T2.ItemSKU
		INNER JOIN TABLE3 T3 ON T1.ItemSKU = T3.ItemSKU
		INNER JOIN TABLE4 T4 ON T1.ItemSKU = T4.ItemSKU
		INNER JOIN TABLE5 T5 ON T1.ItemSKU = T5.ItemSKU
		INNER JOIN TABLE6 T6 ON T1.ItemSKU = T6.ItemSKU
		INNER JOIN TABLE7 T7 ON T1.ItemSKU = T7.ItemSKU
		INNER JOIN TABLE8 T8 ON T1.ItemSKU = T8.ItemSKU
	WHERE T1.[SKU number] = @SKUNumber


END
GO

Open in new window

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 34928695
use this in your sp:

select top 1 * from (
select 1 priority, * from table1 where skuid=@skuid
union
select 2 priority, * from table2 where skuid=@skuid
...
select 8 priority, * from table8 where skuid=@skuid
) x order by priority
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:cdemott33
ID: 34928859
Let me see if I can explain myself better.  I'm trying to do something like this.

@Para_SKU = "1234"

Check Table1
   SELECT name, price, stock FROM table1 Where skuid = @Para_SKU

Did it find a record (True/False)?   FALSE

It's FALSE, so we check Table 2

    SELECT name, price, stock FROM table2 Where skuid = @Para_SKU

Did it find a match (True/False)?   FALSE  

It's FALSE so we check Table 3

    SELECT name, price, stock FROM table3 Where skuid = @Para_SKU

Did it find a match (True/False)?   TRUE

Because it's TRUE, RETURN the data found.  Do not continue.

That's what I'm trying to do.  Once it finds a record in one of the tables the script stops and returns the matched column data it found.

0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 34928884
try like this.
CREATE PROCEDURE usp_SearchSKUNumber
(
	@SKUNumber	INT
)
AS
BEGIN

if exists(select 1 from table1 where SKUNumber = @SKUNumber) select name,price,stock from table1 where SKUNumber = @SKUNumber
else if exists(select 1 from table2 where SKUNumber = @SKUNumber) select name,price,stock from table2 where SKUNumber = @SKUNumber
else if exists(select 1 from table3 where SKUNumber = @SKUNumber) select name,price,stock from table3 where SKUNumber = @SKUNumber
else if exists(select 1 from table4 where SKUNumber = @SKUNumber) select name,price,stock from table4 where SKUNumber = @SKUNumber
else if exists(select 1 from table5 where SKUNumber = @SKUNumber) select name,price,stock from table5 where SKUNumber = @SKUNumber
else if exists(select 1 from table6 where SKUNumber = @SKUNumber) select name,price,stock from table6 where SKUNumber = @SKUNumber
else if exists(select 1 from table7 where SKUNumber = @SKUNumber) select name,price,stock from table7 where SKUNumber = @SKUNumber
else if exists(select 1 from table8 where SKUNumber = @SKUNumber) select name,price,stock from table8 where SKUNumber = @SKUNumber
END
GO

Open in new window

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 34928996
this would be better... did you try this? instead of checking and running lots of uqery?
for example if we find it on last table, above solution will run 16 query!!!! where as this one is only one...

select top 1 * from (
select 1 priority, * from table1 where skuid=@skuid
union
select 2 priority, * from table2 where skuid=@skuid
union
...
union
select 8 priority, * from table8 where skuid=@skuid
) x order by priority

result will be the same...
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34929109
HainKurt -  I think there are pros and cons with two approaches. If the result is in first table, with UNION we are doing lot of unnecessary UNIONs. thoughts?
0
 

Author Closing Comment

by:cdemott33
ID: 34946248
Thank you. The IF EXISTS seams to work well.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 Std. License Key owner or vendor 4 56
Need SSIS project 2 28
SQL Server code help needed 14 32
Need to trim my database size 9 24
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question