Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_MSscriptforeignkeyrestore

  No additional text.


Syntax
create procedure sys.sp_MSscriptforeignkeyrestore (
    @program_name               sysname,
    @constraint_name            sysname,
    @parent_schema              sysname,
    @parent_name                sysname,
    @referenced_object_schema   sysname,
    @referenced_object_name     sysname,
    @is_not_for_replication     bit,
    @is_not_trusted             bit,
    @delete_referential_action  tinyint,
    @update_referential_action  tinyint
    )
as
begin
    set nocount on

    declare @retcode int
    declare @number_of_key_columns int

    declare @script table (line_number int identity primary key, line nvarchar(4000))

    exec @retcode = sys.sp_MSreplcheck_subscribe
    if @@error <> 0 or @retcode <> 0
        return 1

    select @number_of_key_columns = count(*)
      from dbo.MSsavedforeignkeycolumns
     where program_name = @program_name
       and constraint_name = @constraint_name
       and parent_schema = @parent_schema

    insert into @script (line) values (N'alter table ' + quotename(@parent_schema) + N'.' + quotename(@parent_name))
    if @is_not_trusted = 1
    begin
        insert into @script (line) values (N'with nocheck')
    end
    insert into @script (line) values (N'add constraint ' + quotename(@constraint_name) + N' foreign key (' )

    insert into @script (line)
        select quotename(referencing_column_name)
          from dbo.MSsavedforeignkeycolumns
         where program_name = @program_name
           and constraint_name = @constraint_name
           and parent_schema = @parent_schema
           and constraint_column_id = 1

    insert into @script (line)
        select N', ' + quotename(referencing_column_name)
          from dbo.MSsavedforeignkeycolumns
         where program_name = @program_name
           and constraint_name = @constraint_name
           and parent_schema = @parent_schema
           and constraint_column_id > 1
        order by constraint_column_id asc

    insert into @script (line) values (N') references ' + quotename(@referenced_object_schema) + N'.' + quotename(@referenced_object_name) + N' (')

    insert into @script (line)
        select quotename(referenced_column_name)
          from dbo.MSsavedforeignkeycolumns
         where program_name = @program_name
           and constraint_name = @constraint_name
           and parent_schema = @parent_schema
           and constraint_column_id = 1

    insert into @script (line)
        select N', ' + quotename(referenced_column_name)
          from dbo.MSsavedforeignkeycolumns
         where program_name = @program_name
           and constraint_name = @constraint_name
           and parent_schema = @parent_schema
           and constraint_column_id > 1
        order by constraint_column_id asc

    insert into @script (line)
    select N') on delete ' +
        case @delete_referential_action
            when 1 then N'cascade '
            when 2 then N'set null '
            when 3 then N'set default '
            else N'no action '
        end +
        'on update ' +
        case @update_referential_action
            when 1 then N'cascade '
            when 2 then N'set null '
            when 3 then N'set default '
            else N'no action '
        end +
        case when @is_not_for_replication = 1 then N'not for replication'
             else N''
        end

    select line from @script order by line_number asc

end

 
Last revision 2008RTM
See also

  sp_MSrestoresavedforeignkeys (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash