Re: Get Primary Key column
From: Quentin Ran (ab_at_who.com)
Date: 04/08/04
- Next message: Hugo Kornelis: "Re: Temporary Tables"
- Previous message: Brad M.: "Re: How do I add a new column?"
- In reply to: Steve: "Re: Get Primary Key column"
- Next in thread: Steve Amey: "Re: Get Primary Key column"
- Reply: Steve Amey: "Re: Get Primary Key column"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 8 Apr 2004 14:34:03 -0500
Steve,
with the condition that the PK contains only 1 column, you may add the
following to your code:
declare @columnName varchar(55)
select @columnName = c.column_name
from information_schema.constraint_column_usage c
inner join sysobjects o on o.name = c.constraint_name
where o.xtype = 'PK'
and o.parent_obj = object_id(@tableName)
and change
SET @Sql = 'SELECT * FROM ' + @TableName + ' WHERE ' + @columnName + ' = ' +
@Value
Please note that this is accessing both the information_schema view and
system tables.
Quentin
"Steve" <stevea@centurion-ms_RemoveThis_.co.uk> wrote in message
news:eL7iyXXHEHA.2128@TK2MSFTNGP11.phx.gbl...
> 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:
>
> Declare @Sql nvarchar(100)
> SET @Sql = 'SELECT * FROM ' + @TableName + ' WHERE myPrimaryKey = ' +
> @Value
> Exec sp_executesql @sql
>
> 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.
>
> "Adam Machanic" <amachanic@air-worldwide.nospamallowed.com> wrote in
message
> news:eQBK4PXHEHA.1048@TK2MSFTNGP12.phx.gbl...
> > 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.
> >
> >
> > "Steve" <stevea@centurion-ms_RemoveThis_.co.uk> wrote in message
> > news:OaaV8MXHEHA.2260@TK2MSFTNGP09.phx.gbl...
> > > 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.
> > >
> > >
> >
> >
>
>
- Next message: Hugo Kornelis: "Re: Temporary Tables"
- Previous message: Brad M.: "Re: How do I add a new column?"
- In reply to: Steve: "Re: Get Primary Key column"
- Next in thread: Steve Amey: "Re: Get Primary Key column"
- Reply: Steve Amey: "Re: Get Primary Key column"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|