Please help me with this Dynamic Sql Query

  • EXEC sp_MSforeachtable '

    declare @tblname nvarchar(255);

    declare @schema nvarchar(255);

    declare @Database nvarhcar(255);

    declare @dsql nvarchar(255);

    SET @tblname = PARSENAME("?",1);

    SET @schema =N'dbo';

    set @Database=N'Adventureworks';

    select @sql = N'EXEC [dbo].[usp_DropPKFKConstarints]'+QUOTENAME(@Database,'''')+N','+QUOTENAME(@schema,'''')+N','+QUOTENAME(@tblname,'''') ;

    Print(@sql);

    '

    can you please help me to fix the issue with this Query

  • sql.kishore1 (2/15/2012)


    can you please help me to fix the issue with this Query

    What is the issue? EDIT: Before you ask, no, I didn't run it to see. I don't run anything from the web that includes sp_MSForAnything.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Try this instead:

    select

    'EXEC [dbo].[usp_DropPKFKConstraints] N''' + TABLE_CATALOG + ''',N''' + TABLE_SCHEMA + ''',N''' + TABLE_NAME + ''';'

    from information_schema.tables

  • 🙂

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • It Did not worked

  • sql.kishore1 (2/16/2012)


    It Did not worked

    What does that mean? Did it not do what you expected? Did it do something else? Did you get an error message? Did your computer blowup? Simply saying it didn't work does not give anybody much to go on. This is like taking you car to the shop and telling them there is something wrong with your car. You have to provide some kind of information about what is happening if you want somebody to be able to help you.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

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

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