Get Primary Key column  
Author Message
jowa





PostPosted: Thu Apr 08 09:15:50 CDT 2004 Top

SQL Server Developer >> Get Primary Key column

Hi all

Is there a way in SQL to reference the Primary Key column without using the
name of the column?

Something like: Select * From Table1 Where myPrimaryKey = '1'

Kind Regards,
Steve.

SQL Server273  
 
 
Adam





PostPosted: Thu Apr 08 09:15:50 CDT 2004 Top

SQL Server Developer >> Get Primary Key column No, and a primary key can also be a composite of multiple columns, so that
wouldn't be possible anyway...

What is it you're trying to accomplish? If you can post more details,
perhaps we can help you find a solution.




> Hi all
>
> Is there a way in SQL to reference the Primary Key column without using
the
> name of the column?
>
> Something like: Select * From Table1 Where myPrimaryKey = '1'
>
> Kind Regards,
> Steve.
>
>


 
 
Steve





PostPosted: Thu Apr 08 09:30:56 CDT 2004 Top

SQL Server Developer >> Get Primary Key column Hi Adam

All my Primary Keys are composed of 1 column, I thought I read somewhere
that you could do what I'm after, but I guess not.

I would like to write 1 stored procedure that can perform the same function
on any table depending on the parameters I pass to it. For example, a Stored
Proc takes a table name and a value as input parameters, and in the proc I
execute the following statement:



@Value


Using this I do not have to specify the actual name of the PK column, so it
makes the proc more general. This is only 1 example of how I would use it,
there are more. Basically, the tables only have 1 primary key and I thought
that there was a SQL name that I could use to refer to that column. I know
that I could get the name of the PK from the system tables, but a 1 line
solution is much more preferable :o)

Thanks for your quick response!

Kind Regards,
Steve.



> No, and a primary key can also be a composite of multiple columns, so that
> wouldn't be possible anyway...
>
> What is it you're trying to accomplish? If you can post more details,
> perhaps we can help you find a solution.
>
>


> > Hi all
> >
> > Is there a way in SQL to reference the Primary Key column without using
> the
> > name of the column?
> >
> > Something like: Select * From Table1 Where myPrimaryKey = '1'
> >
> > Kind Regards,
> > Steve.
> >
> >
>
>


 
 
Quentin





PostPosted: Thu Apr 08 14:34:03 CDT 2004 Top

SQL Server Developer >> Get Primary Key column Steve,

with the condition that the PK contains only 1 column, you may add the
following to your code:




from information_schema.constraint_column_usage c
inner join sysobjects o on o.name = c.constraint_name
where o.xtype = 'PK'


and change


@Value

Please note that this is accessing both the information_schema view and
system tables.

Quentin




> Hi Adam
>
> All my Primary Keys are composed of 1 column, I thought I read somewhere
> that you could do what I'm after, but I guess not.
>
> I would like to write 1 stored procedure that can perform the same
function
> on any table depending on the parameters I pass to it. For example, a
Stored
> Proc takes a table name and a value as input parameters, and in the proc I
> execute the following statement:
>




>
> Using this I do not have to specify the actual name of the PK column, so
it
> makes the proc more general. This is only 1 example of how I would use it,
> there are more. Basically, the tables only have 1 primary key and I
thought
> that there was a SQL name that I could use to refer to that column. I know
> that I could get the name of the PK from the system tables, but a 1 line
> solution is much more preferable :o)
>
> Thanks for your quick response!
>
> Kind Regards,
> Steve.
>

message

> > No, and a primary key can also be a composite of multiple columns, so
that
> > wouldn't be possible anyway...
> >
> > What is it you're trying to accomplish? If you can post more details,
> > perhaps we can help you find a solution.
> >
> >


> > > Hi all
> > >
> > > Is there a way in SQL to reference the Primary Key column without
using
> > the
> > > name of the column?
> > >
> > > Something like: Select * From Table1 Where myPrimaryKey = '1'
> > >
> > > Kind Regards,
> > > Steve.
> > >
> > >
> >
> >
>
>


 
 
Steve





PostPosted: Sat Apr 10 04:25:29 CDT 2004 Top

SQL Server Developer >> Get Primary Key column Hi Quentin

Thanks for that! I don't know if this is a recommended way to program, but
it would cut down the amount of maintenance work I'd have to do.

Kind Regards,
Steve.



> Steve,
>
> with the condition that the PK contains only 1 column, you may add the
> following to your code:
>

>

> from information_schema.constraint_column_usage c
> inner join sysobjects o on o.name = c.constraint_name
> where o.xtype = 'PK'

>
> and change
>

+

>
> Please note that this is accessing both the information_schema view and
> system tables.
>
> Quentin
>
>


> > Hi Adam
> >
> > All my Primary Keys are composed of 1 column, I thought I read somewhere
> > that you could do what I'm after, but I guess not.
> >
> > I would like to write 1 stored procedure that can perform the same
> function
> > on any table depending on the parameters I pass to it. For example, a
> Stored
> > Proc takes a table name and a value as input parameters, and in the proc
I
> > execute the following statement:
> >




> >
> > Using this I do not have to specify the actual name of the PK column, so
> it
> > makes the proc more general. This is only 1 example of how I would use
it,
> > there are more. Basically, the tables only have 1 primary key and I
> thought
> > that there was a SQL name that I could use to refer to that column. I
know
> > that I could get the name of the PK from the system tables, but a 1 line
> > solution is much more preferable :o)
> >
> > Thanks for your quick response!
> >
> > Kind Regards,
> > Steve.
> >

> message

> > > No, and a primary key can also be a composite of multiple columns, so
> that
> > > wouldn't be possible anyway...
> > >
> > > What is it you're trying to accomplish? If you can post more details,
> > > perhaps we can help you find a solution.
> > >
> > >


> > > > Hi all
> > > >
> > > > Is there a way in SQL to reference the Primary Key column without
> using
> > > the
> > > > name of the column?
> > > >
> > > > Something like: Select * From Table1 Where myPrimaryKey = '1'
> > > >
> > > > Kind Regards,
> > > > Steve.
> > > >
> > > >
> > >
> > >
> >
> >
>
>