Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to use unique identifier column where clause of SQL Server 2000? Expand / Collapse
Author
Message
Posted Sunday, April 27, 2008 7:29 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 6, 2009 3:49 AM
Points: 2, Visits: 6
Hi,

I got 1 problem regarding uniqueidentifier column that how do we select values from table where uniqueidentifier columns matches the value.

i use this query

select sCode_value from Codes where
gCode_Id = '853C63ED-D7CC-4E86-965E-F425A53E1BA2'

note: gCode_ID is UnqiueIdentifier column

I also tried below query

select sCode_value from Codes where
gCode_Id = Cast('853C63ED-D7CC-4E86-965E-F425A53E1BA2' as UnqiueIdentifier)

but no luck

any one helps me

thanks
Post #491121
Posted Sunday, April 27, 2008 7:50 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, December 17, 2014 9:58 AM
Points: 2,281, Visits: 4,241
Your problem is not clear as "no luck" is not very meaningful. Do you mean that no rows are found ? If so, check the value of the uniqueidentifier that is being used.

use tempdb
go
create table FOO
(FooId uniqueidentifier not null default newid()
,FooDescr varchar(255) not null
, constraint FOO_P primary key (FooId)
)
insert into FOO (FooDescr)
select 'First' union select 'Second'

select * from FOO
-- replace the value of FooId based on the select
select * from Foo where FooId = '9E3DF8B0-120B-47B4-B150-375B23EE9DDE'



SQL = Scarcely Qualifies as a Language
Post #491124
Posted Monday, April 28, 2008 1:52 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 8:08 AM
Points: 2,365, Visits: 1,846
Hi

This query "select sCode_value from Codes where
gCode_Id = '853C63ED-D7CC-4E86-965E-F425A53E1BA2' " which you have mentioned will work.

As Carl said if no rows are being returned check whether the uniqueidentifier value is being used.

BTW why do you want to use CAST. Is'nt the column datatype uniqueidentifier ?


"Keep Trying"
Post #491226
Posted Monday, April 28, 2008 7:05 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Yesterday @ 10:56 PM
Points: 442, Visits: 936
I could not replicate your problem. I tried the following:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Table1]
GO

CREATE TABLE [dbo].[Table1] (
[pk] uniqueidentifier ROWGUIDCOL NOT NULL,
[ln_no] [int] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Table1] WITH NOCHECK ADD
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
[pk]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Table1] ADD
CONSTRAINT [DF_Table1_pk] DEFAULT (newid()) FOR [pk]
GO


INSERT INTO Table1 (ln_no) VALUES (1)

--Viewing the record in Enterprise Manager -> Tables -> Open Table -> All Rows:
--the uniqueidentifier column is stored as
{2F240FEA-0116-4915-9382-CFA4B97B9297} --NOTE curly braces {} !
--whereas the query analyzer displays
2F240FEA-0116-4915-9382-CFA4B97B9297 -- NO braces

--1. From Query Analyzer, BOTH statements, with or without braces, WORK
INSERT INTO Table1 (ln_no) values (1)
SELECT * FROM Table1 WHERE pk = '{2F240FEA-0116-4915-9382-CFA4B97B9297}'
SELECT * FROM Table1 WHERE pk = '2F240FEA-0116-4915-9382-CFA4B97B9297'

pk ln_no
2F240FEA-0116-4915-9382-CFA4B97B9297 1


--2. Checking off IsRowGuid produces the same results
INSERT INTO Table1 (ln_no) values (2)
SELECT * FROM Table1 WHERE pk = '{707A6054-A363-4263-8CB7-F0776A518DA8}'
SELECT * FROM Table1 WHERE pk = '707A6054-A363-4263-8CB7-F0776A518DA8'

pk ln_no
707A6054-A363-4263-8CB7-F0776A518DA8 2


--Removing the default NEWID() property and entering it explicitly STILL WORKS
INSERT INTO Table1 (pk, ln_no) Select NEWID(), 3
SELECT * FROM Table1 WHERE pk = '{DE269F48-45DC-4BBB-8FD7-9A474E2906D6}'
SELECT * FROM Table1 WHERE pk = 'DE269F48-45DC-4BBB-8FD7-9A474E2906D6'

pk ln_no
DE269F48-45DC-4BBB-8FD7-9A474E2906D6 3

Tp help ypu further, you have to suly more info. Script your table and insert values.

Post #491353
Posted Monday, April 28, 2008 1:39 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 6, 2009 3:49 AM
Points: 2, Visits: 6
Thanks a lot to all of you for your kind help. I apprecaite your kind and quick help that will solve my problem.

Hopefully in the future i'll get have same type of reponses from you\

Thanks once again
Regards
Sohail
Post #491689
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse