none
Call Primary Key in SQL statement

    Question

  • Is it possible to create an SQL statement somthing like "WHERE PRIMARY KEY is something"??  Primary Key being the primary key of the table.  I need to have it find it in the SQL statement because it can change depending on which table I pass to the function.
    Wednesday, August 16, 2006 7:37 PM

Answers

  • You could try to use SqlDataReader.GetSchemaTable() to retrieve key info from a table. (You have to specify CommandBehavior.KeyInfo).
    Thursday, August 17, 2006 6:47 AM

All replies

  • Pretty sure the answer is no to this one.
    Wednesday, August 16, 2006 7:39 PM
  • Although there is not a keyword such as "PrimaryKey" to get the primary key of a table there are methods to do so...ie...Returning the schema for the table and getting the primary key field name
    Wednesday, August 16, 2006 8:30 PM
  • There is no generic definition that defines PRIMARY KEY, so you will have to know what field is the primary key.  The proper syntax is to say:

    SELECT fielda, fieldb, key FROM myTable WHERE key = 'something'

    Also as a side note, you could also say WHERE fielda = 'somevalue' even though it is not a primary key.  It just would not be as performant without adding an index on that field.

    Wednesday, August 16, 2006 8:38 PM
  • How would I go about returning the schema? 

    What I'm trying to do is convert an old VB6 program that has a function that utilizes a global connection to a database and then an ADODB recordset in certain function and does a seek method on the primary key.  However the primary key will change according to what table is passed to the function.

    Thursday, August 17, 2006 2:25 AM
  • You could try to use SqlDataReader.GetSchemaTable() to retrieve key info from a table. (You have to specify CommandBehavior.KeyInfo).
    Thursday, August 17, 2006 6:47 AM