Re: Table has identity field
From: Adam Machanic (amachanic_at_air-worldwide.nospamallowed.com)
Date: 03/29/04
- Next message: Adam Machanic: "Re: Error using a derived table"
- Previous message: Stephen Russell: "can a UDF return a table?"
- In reply to: Seal: "Table has identity field"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 29 Mar 2004 12:56:51 -0500
IF EXISTS (select * from syscolumns where id=object_id('mytable') and
objectproperty(id, 'tablehasidentity') = 1)
BEGIN
...
END
Replace 'mytable' with the name of your table...
"Seal" <sealtielp@afex.com> wrote in message
news:ur3ipUbFEHA.1600@tk2msftngp13.phx.gbl...
> Hi everyone,
>
>
>
> Is there a fast and reliable way to determinate if a table has an identity
> field?
>
> I'm working in a transfer process between servers and from time to time I
> have to transfer records between tables with identity fields. The process
> uses dynamic sql widely because is driven by some configurations tables.
>
>
>
> This is part of the code:
>
> After getting the list of fields that I need to transfer and marking those
> records to update or insert (All the records from the remote server are
> already in a temp table):
>
> ---------------
>
> Set @sql = 'Insert into ' + @sTableLocal + '(' + @sFieldsL + ') '
>
> Set @sql = @sql + 'Select ' + @sFieldsR + ' From ' + @sTempTable + ' Where
> tfrbExists = 0'
>
> exec sp_Executesql @sql
>
> ----------------
>
>
>
> This work fine for tables without identity field however fails if I ever
add
> a table with identity field:
>
> I would like to do something like this
>
> ----------------
>
> -- For those tables with Identity fields
>
> If funTableHasIdentityField(@sTableLocal) = 1 -- Or something like that
>
> Begin
>
> Set @sql = 'SET IDENTITY_INSERT ' + @ sTableLocal + ' ON '
>
> Set @sql = @sql + ' Insert into ' + @ sTableLocal + '(' +
> @sFieldsL + ') '
>
> Set @sql = @sql + 'Select ' + @sFieldsR + ' From ' + @sTempTable + ' Where
> tfrbExists = 0'
>
> End
>
> Else
>
> Begin
>
> Set @sql = 'Insert into ' + @ sTableLocal + '(' + @sFieldsL +
')
> '
>
> Set @sql = @sql + 'Select ' + @sFieldsR + ' From ' + @sTempTable + ' Where
> tfrbExists = 0'
>
> End
>
> exec sp_Executesql @sql
>
> ---------------
>
>
>
> What will be the best way to accomplish "funTableHasIdentityField"?
>
> Any sugestions? Ideas?
>
>
>
> Thanks in advance.
>
>
>
>
>
>
- Next message: Adam Machanic: "Re: Error using a derived table"
- Previous message: Stephen Russell: "can a UDF return a table?"
- In reply to: Seal: "Table has identity field"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|