SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to use unique identifier column where clause of SQL Server 2000?


How to use unique identifier column where clause of SQL Server 2000?

Author
Message
sohail29
sohail29
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 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
Carl Federl
Carl Federl
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3382 Visits: 4350
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
ChiragNS
ChiragNS
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3509 Visits: 1865
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"
J-440512
J-440512
Right there with Babe
Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)

Group: General Forum Members
Points: 769 Visits: 949
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.
sohail29
sohail29
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search