Groups | Blog | Home
all groups > sql server programming > june 2006 >

sql server programming : Second try: Need a little help with a sql query


--CELKO--
6/15/2006 2:50:44 PM
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

[quoted text, click to view]

No, this is a totally useless narrative. If you were not so rude,
would you have had a table like:

put the culture code into the local tables. Think about it; where is
it a default?? LOCALLY! Do a REFERENCES and a DEFAULT clause in the
DDL.
Anith Sen
6/15/2006 5:05:35 PM
Please post your table structures ( as CREATE TABLE statements ), sample
data ( INSERT statements ) and expected results so that others can better
understand your requirements. For details refer to : www.aspfaq.com/5006

--
Anith

MatsL NO[at]SPAM newsgroups.nospam
6/15/2006 11:22:07 PM
My post was, umm... lost or something. Didn't show up in my client
anyway. So I do a second post. Sorry if this is posted twice.

Here it is:

Hi, I hope this is the right group to ask this kind of question, but I
saw some other people asking for help with queries so here it goes.

I have a view created from two tables. One holding base data and the
other localized data. It looks something like this (simplified version)

BaseTable
Id (Pk)
SomeData

LocalizedTable
Id (Pk)
CultureID (Pk)
LocalizedData

I also have a third table specifying the available cultures

CultureTable
CultureID (Pk)
IsDefault

There is always one culture in the CultureTable that is the default, and
for each row in the BaseTable there is a row in the LocalizedTable that
has the default culture.

What I want to do is to write a query that does a join on BaseTable and
LocalizedTable to which I want to pass a parameter @CultureID that will
select all rows from BaseTable and join in rows from LocalizedTable
where CultureID = @CultureID or CultureID = (SELECT CultureID from
CultureTable where IsDefault = 1).

I'm really quite stumped as how I should do it. I'm able to get all rows
with that where clause but I only want the default culture rows for the
ones that are missing from the specified culture.

Any help with this would be greatly appreciated!

Thanks in advance,
MatsL NO[at]SPAM newsgroups.nospam
6/16/2006 12:00:00 AM
Thanks for the reply. Next time I'll be more thorough with my question.


[quoted text, click to view]
MatsL NO[at]SPAM newsgroups.nospam
6/16/2006 12:00:00 AM
Thanks Wei and thank you Roy!
I think my client may be crazy but I can't find my other post or Roy's
response.

I'll look into using COALESCE to retrieve the results I want.

Best regards,
Mats Lycken

[quoted text, click to view]
weilu NO[at]SPAM online.microsoft.com
6/16/2006 12:00:00 AM
Hi Mats,

Thank you for the update. If you have any questions or concerns, please
feel free to let me know.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
weilu NO[at]SPAM online.microsoft.com
6/16/2006 4:56:55 AM
Hi Mats,

Thank you for your posting!

I have noticed that you have posted the same questions in this newsgroup.
And there is a customer answered your problem there. I agree with him and I
will post the answer here:

From: Roy Harvey <roy_harvey@snet.net>
Subject: Re: Need help with a query
Date: Thu, 15 Jun 2006 18:05:12 -0400
Newsgroups: microsoft.public.sqlserver.programming

SELECT B.*, L.*,
COALESCE(L.CultureID,
(select CultureID from CultureTable
where IsDefault = 1))
FROM BaseTable as B
LEFT OUTER
JOIN LocalizedTable as L
ON B.Id = L.Id
AND L.CultureID = @CultureID

A column name of Id is pretty horrible, by the way. It should be
qualified by something, BaseId perhaps.

Roy Harvey
Beacon Falls, CT

Hope this will be helpful!

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
MatsL NO[at]SPAM newsgroups.nospam
6/21/2006 12:00:00 AM
Hi again Wei,
I know this is kind of late to write a reply to this thread but I just
wanted to say I'm impressed with the kind of support that is offered
here in this thread. I just expected to get a hint to as what I should
do, not a complete query, a very pleasant surprise indeed.

I thought that as a practice for future postings I would paste my DDL
and the solution I found to my problem, I actually found the solution in
another post.

The trick was to use NOT EXISTS in the where clause, like this:
WHERE A.CultureID = @CultureID OR
(
A.CultureID = (SELECT CultureID from cms_Language WHERE IsDefault = 1)
AND NOT EXISTS
(
SELECT * FROM vw_cms_WSCategory as B WHERE B.CultureID =
@CultureID AND A.CategoryID = B.CategoryID
)
)

You can try run the sql snippet to create the tables and populate them
and finally run execute the sproc with the command below to see the result.

The post became quite lengthy but I can really agree that there is not
much room for misunderstanding when being this thorough.

Thanks for the replies and I look forward to making posts to this groups
in the future.

Best regards,
Mats Lycken

btw, I've already changed the CultureID nvarchar(10) to a char(5)... ;)



/* Command to execute begins (Use this to execute the sproc) */

DECLARE @RC int
DECLARE @CultureID nvarchar(10)

SET @CultureID = 'en-us'

EXECUTE @RC = [vw_cms_WSCategory_Sel]
@CultureID

/* Command to execute ends */

/* ------ SQL snippet begins, copy from here... */

/*
*
* Here is the DDL for my two tables:
*
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[cms_WSCategory](
[CategoryID] [uniqueidentifier] NOT NULL,
[Published] [bit] NOT NULL,
[CreatedBy] [uniqueidentifier] NOT NULL,
[Created] [datetime] NOT NULL,
[Parent] [uniqueidentifier] NULL,
[SortOrder] [int] NOT NULL CONSTRAINT [DF_cms_WSCategory_SortOrder]
DEFAULT ((0)),
[OverrideLink] [nvarchar](256) COLLATE Finnish_Swedish_CI_AS NULL,
CONSTRAINT [PK_cms_WSCategory] PRIMARY KEY CLUSTERED
(
[CategoryID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[cms_WSCategoryLocalized] Script Date:
06/21/2006 13:36:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[cms_WSCategoryLocalized](
[CategoryID] [uniqueidentifier] NOT NULL,
[CultureID] [nvarchar](10) COLLATE Finnish_Swedish_CI_AS NOT NULL,
[DisplayName] [nvarchar](64) COLLATE Finnish_Swedish_CI_AS NULL,
[LastModifiedBy] [uniqueidentifier] NOT NULL,
[LastModified] [datetime] NOT NULL,
[DisplayHTML] [nvarchar](1024) COLLATE Finnish_Swedish_CI_AS NULL,
CONSTRAINT [PK_cms_WSCategoryLocalized] PRIMARY KEY CLUSTERED
(
[CategoryID] ASC,
[CultureID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[cms_WSCategoryLocalized] WITH CHECK ADD CONSTRAINT
[FK_cms_WSCategoryLocalized_cms_WSCategory] FOREIGN KEY([CategoryID])
REFERENCES [dbo].[cms_WSCategory] ([CategoryID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[cms_WSCategoryLocalized] CHECK CONSTRAINT
[FK_cms_WSCategoryLocalized_cms_WSCategory]
GO
/*
*
* Create the language table
*
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[cms_Language](
[DisplayName] [nvarchar](50) COLLATE Finnish_Swedish_CI_AS NOT NULL,
[CultureID] [nvarchar](10) COLLATE Finnish_Swedish_CI_AS NOT NULL,
[IsDefault] [bit] NOT NULL CONSTRAINT [DF_cms_Language_IsDefault]
DEFAULT ((0))
) ON [PRIMARY]
GO
/*
*
* Here are some inserts for the base table:
*
*/
INSERT INTO [cms_WSCategory]
VALUES('3B217318-2C34-4536-9239-2F7157AAFB39',1,'00000000-0000-0000-0000-000000000000','Dec
20 2005 11:15:39:987AM','00000000-0000-0000-0000-000000000000',0,NULL)
GO
INSERT INTO [cms_WSCategory]
VALUES('41D0DFD3-BEF2-4215-BF3A-5387FEB9774D',1,'00000000-0000-0000-0000-000000000000','Dec
20 2005 11:14:17:440AM','00000000-0000-0000-0000-000000000000',0,'skiplink')
GO
/*
*
* Here are some inserts for the localized table:
*
*/
INSERT INTO [cms_WSCategoryLocalized]
VALUES('3B217318-2C34-4536-9239-2F7157AAFB39','nn-no','Oppbygget
gulv','00000000-0000-0000-0000-000000000000','Dec 20 2005
11:15:39:987AM',NULL)
GO
INSERT INTO [cms_WSCategoryLocalized]
VALUES('41D0DFD3-BEF2-4215-BF3A-5387FEB9774D','en-us','Carpets','00000000-0000-0000-0000-000000000000','Dec
20 2005 11:14:17:000AM','')
GO
INSERT INTO [cms_WSCategoryLocalized]
VALUES('41D0DFD3-BEF2-4215-BF3A-5387FEB9774D','nn-no','Tepper','00000000-0000-0000-0000-000000000000','Dec
20 2005 11:14:17:440AM','<span style="font-weigth: bold; color:
Red;">Tepper</span>')
GO
/*
*
* Here are some inserts for the language table:
*
*/
INSERT INTO [cms_Language] VALUES('English','en-us',0)
GO
INSERT INTO [cms_Language] VALUES('Norsk','nn-no',1)
GO
/*
*
* Create a view from the two tables
*
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vw_cms_WSCategory]
AS
SELECT dbo.cms_WSCategory.CategoryID, dbo.cms_WSCategory.Published,
dbo.cms_WSCategory.CreatedBy, dbo.cms_WSCategory.Created,
dbo.cms_WSCategoryLocalized.CultureID,
dbo.cms_WSCategoryLocalized.DisplayName,
dbo.cms_WSCategoryLocalized.LastModifiedBy,
dbo.cms_WSCategoryLocalized.LastModified,
dbo.cms_WSCategory.Parent, dbo.cms_WSCategory.SortOrder,
dbo.cms_WSCategoryLocalized.DisplayHTML,
dbo.cms_WSCategory.OverrideLink
FROM dbo.cms_WSCategory INNER JOIN
dbo.cms_WSCategoryLocalized ON
dbo.cms_WSCategory.CategoryID = dbo.cms_WSCategoryLocalized.CategoryID

GO
/*
*
* Here is the stored procedure that solved my issue:
*
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[vw_cms_WSCategory_Sel]
(
@CultureID nvarchar(10)
)
AS



SELECT *
FROM vw_cms_WSCategory as A
WHERE A.CultureID = @CultureID OR
(
A.CultureID = (SELECT CultureID from cms_Language WHERE IsDefault = 1)
AND NOT EXISTS
(
SELECT * FROM vw_cms_WSCategory as B WHERE B.CultureID =
@CultureID AND A.CategoryID = B.CategoryID
)
)
ORDER BY SortOrder ASC, DisplayName ASC
RETURN
GO

/* ------ SQL snippet ends, ...to here. */

[quoted text, click to view]
weilu NO[at]SPAM online.microsoft.com
6/22/2006 8:21:33 AM
Hi Mats,

Appreciate your update and response. I am glad to hear that the problem has
been fixed. If you have any other questions or concerns, please do not
hesitate to contact us. It is always our pleasure to be of assistance.

Have a nice day!

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button