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

  • 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

  • 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

    (FooIduniqueidentifiernot null default newid()

    ,FooDescrvarchar(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

  • 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"

  • 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.

  • 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

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply