Need to make this Stored Procedure 2005 compatible

  • I need to quickly make this SP compatible with SQL 2005 and am struggling. I have alot of catching up to do.

    Basically, this SP checks for FK dependencies in a database. Any help is verry much appreciated!

    --------------------------------------------------------

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_NULLS ON

    GO

    ALTER  Procedure aes.Check_Dependent_Rows_Exist

    (@RowID int,

    @has_rows int OUTPUT

    )

    AS

    BEGIN

     DECLARE @Colname varchar(200), @Tablename varchar(200)

     DECLARE @cnt int

     DECLARE @temp_row int

     DECLARE @owner varchar(25)

     DECLARE @ownerid int

     DECLARE @lstrSql nvarchar(2000)

     -- #1: declare cursor for maximum performance

     DECLARE lcur CURSOR LOCAL FORWARD_ONLY KEYSET READ_ONLY FOR

     

          SELECT syscolumns.Name, OBJECT_NAME(fkeyid) AS FkeyTableName

          FROM sysreferences

      INNER JOIN syscolumns ON sysreferences.fkeyid=syscolumns.id AND fkey1=syscolumns.colid

          WHERE OBJECT_NAME(rkeyid)= 'customer'

     OPEN lcur

      CREATE TABLE #Temp (DependentRows int)

      --  #2: only return a bit indicating if dependant rows exist or not

      SET @has_rows = 0

      

     FETCH NEXT FROM lcur INTO @Colname,@Tablename

     

      WHILE @@FETCH_STATUS = 0

          BEGIN

       SET @temp_row = 0  

       

       SELECT @ownerid = uid from sysobjects where name = @Tablename

       SELECT @owner = [name] from sysusers where uid = @ownerid   

       SET @lstrSql= 'insert into #Temp Select DependentRows = Count(' + @Colname + ') from ' + @owner + '.' + @TableName + ' where ' +

       @Colname + ' =' + CAST(@RowID AS VARCHAR(16)) + ''

       --print @lstrSql

       EXEC (@lstrSql)

       SELECT @temp_row = ISNULL(DependentRows,0) FROM #Temp

       IF @temp_row > 0

       BEGIN

       -- #3: stop processing as soon as dependant rows are found to exist

       SET @has_rows = 1

       BREAK

       END

       

      FETCH NEXT FROM lcur INTO @Colname,@TableName

         END

      deallocate  lcur

    END

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    SET ANSI_NULLS ON

    GO

    -----------------------------------------------------------------------

  • What is not working with this proc. It should still run in SQLServer 2005...

  • Sorry, should have included the error

    -------------------------------------------------------------------

    Server: Msg 208, Level 16, State 1, Line 1

    Invalid object name 'dbo.order_detail'.

    Server: Msg 208, Level 16, State 1, Line 1

    Invalid object name 'dbo.invoice_header'.

    Server: Msg 208, Level 16, State 1, Line 1

    Invalid object name 'dbo.order_header'.

    Server: Msg 208, Level 16, State 1, Line 1

    Invalid object name 'dbo.payment'.

    Server: Msg 208, Level 16, State 1, Line 1

    Invalid object name 'dbo.cash_on_account'.

     

    (1 row(s) affected)

     

    Cannot grant, deny, or revoke permissions to sa, dbo, information_schema, sys, or yourself.

    1

    --------------------------------------------------------------

    Thank you

  • did you upgrade the server or move the DBs to a new server?

    does that table exist?

    who is owner?

    are server logins and db users in synch?


    Cheers,

    Todd

  • The server was upgraded.

    The tables do exist but the owner is not dbo. I think I need to change the proc to pull the user from Sysusers ?? Not sure. It works fine on the same db in 2000.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply