Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 25th, 2003, 07:50 AM
Authorized User
 
Join Date: Jun 2003
Location: , Quebec, Canada.
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simplify the stored proc

Is there a way to simplify the stored proc so it will used only one query?

This is what I have so far

IF (SELECT COUNT(DataID) FROM Data_Companies WHERE CompanyID = @CompanyID) > 0
BEGIN
    SELECT Data.DataID, Data.DataName, ISNULL(Data_Labels_Companies.Label, Data_Labels.Label)
    FROM Data_Companies INNER JOIN Data
            ON Data_Companies.DataID = Data.DataID
            AND Data_Companies.CompanyID = @CompanyID
        INNER JOIN Data_Labels
            ON Data.DataID = Data_Labels.DataID
            AND Data_Labels.LanguageID = @LanguageID
        LEFT OUTER JOIN Data_Labels_Companies
            ON Data_Labels.DataID = Data_Labels_Companies.DataID
            AND Data_Labels_Companies.LanguageID = @LanguageID
            AND Data_Labels_Companies.Company = @CompanyID
    ORDER BY Data_Companies.OrderSeq
END

ELSE
BEGIN
    SELECT Data.DataID, Data.DataName, ISNULL(Data_Labels_Companies.Label, Data_Labels.Label)
    FROM Data INNER JOIN Data_Labels
            ON Data.DataID = Data_Labels.DataID
            AND Data_Labels.LanguageID = @LanguageID
        LEFT OUTER JOIN Data_Labels_Companies
            ON Data_Labels.DataID = Data_Labels_Companies.DataID
            AND Data_Labels_Companies.LanguageID = @LanguageID
            AND Data_Labels_Companies.Company = @CompanyID
    ORDER BY Data.OrderSeq
END

Stéphane Lajoie
__________________
Stéphane

A programmer is a device that transform coffee in code lines

\"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.\" Rich Cook
Reply With Quote
  #2 (permalink)  
Old July 27th, 2003, 06:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Sydney, NSW, Australia.
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The code below assumes that Data.OrderSeq and Data_Companies.OrderSeq are the same data type. If they aren't you could cast both to varChar.

Also you could probably avoid having to use the variable @Exists, but this looks cleaner to me.
Code:
DECLARE @Exists bit

SELECT @Exists = CAST(COUNT(DataID) AS bit)
FROM Data_Companies
WHERE CompanyID = @CompanyID

SELECT Data.DataID, Data.DataName,
    ISNULL(Data_Labels_Companies.Label, Data_Labels.Label)
FROM Data_Companies
    INNER JOIN Data    ON
    Data_Companies.DataID = Data.DataID
    AND Data_Companies.CompanyID = @CompanyID
    INNER JOIN Data_Labels ON
    Data.DataID = Data_Labels.DataID
    AND Data_Labels.LanguageID = @LanguageID
    LEFT OUTER JOIN Data_Labels_Companies ON
    Data_Labels.DataID = Data_Labels_Companies.DataID AND
    Data_Labels_Companies.LanguageID = @LanguageID AND
    Data_Labels_Companies.Company = @CompanyID
ORDER BY CASE @Exists
             WHEN 1 THEN Data_Companies.OrderSeq
             ELSE Data.OrderSeq
         END
regards
David Cameron
Reply With Quote
  #3 (permalink)  
Old August 5th, 2003, 10:14 AM
Authorized User
 
Join Date: Jun 2003
Location: , Quebec, Canada.
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks David for your response. You query is good but I think you miss understood what I want. I'll try to make this more clear to understand.

This is the definition of my table:

Data: DataID [int], DataName [varchar], OrderSeq [int]
Containts all the data for the application.

DataCompanies: DataID [int], CompanyID [int], OrderSeq [int]
Containts the custom data for a given company

DataLabels: DataID [int], LanguageID [int], Label [varchar]
Containts the label to display in for each language

DataLabelsCompanies: DataID [int], LanguageID [int], CompanyID [int], Label [varchar]
Containts the custom label for a given company

This is an exemple of what the table may containt:

Data
1,Mister,1
2,Misses,2
3,Doctor,4
4,Miss,3

DataCompanies
1,1,3
2,1,1
4,1,2
1,3,4
2,3,2
3,3,1
4,3,3

DataLabels
1,1,Mr
1,2,M
2,1,Mrs
2,2,Mdm
3,1,Dr
3,2,Doc
4,1,Ms
4,2,Mdl

DataLabels
4,1,3,Miss

If the @CompanyID is equal to 1 and LanguageID to 2, I should get
2,Misses,M
4,Miss,Mdl
1,Mister,M

If the @CompanyID is equal to 2 and LanguageID to 1, I should get
1,Mister,Mr
2,Misses,Mrs
4,Miss,Ms
3,Doctor,Dr

If the @CompanyID is equal to 3 and LanguageID to 1, I should get
3,Doctor,Dr
2,Misses,Mrs
4,Miss,Miss
1,Mister,Mr


Is there an other way to get this data from my stored proc.

Stéphane Lajoie
Reply With Quote
  #4 (permalink)  
Old August 5th, 2003, 08:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Sydney, NSW, Australia.
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You've confused me. I have to admit I don't understand your table structure. When I posted my reply as far as I could tell it would do exactly what your earlier query did, except that it did it without the IF. My understanding was that you wanted a dynamic where clause based on whether some rows existed.

On the tables, have you posted all the columns in these tables? Which ones are primary keys (including the composite keys)? Which ones are foreign keys?

regards
David Cameron
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Proc vs Functions. carumuga SQL Server 2000 1 January 7th, 2008 06:17 AM
Stored Proc that returns a value elygp SQL Server 2000 4 May 9th, 2007 01:05 AM
Calling an insert stored proc from a select stored dzitam SQL Language 10 April 2nd, 2007 12:39 PM
How to get value from stored proc busybee ASP.NET 1.0 and 1.1 Basics 4 April 2nd, 2006 12:06 AM
optional where in stored proc david_ste SQL Server ASP 2 October 27th, 2005 07:16 AM



All times are GMT -4. The time now is 06:33 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.