Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2012
    Posts
    6

    Unanswered: Dynamic Sql using sql server 2008

    Hey I am using sql server 2008 and I need to complete this for a class I am taking and have no exp with dynamic sql

    write a script that uses dynamic SQL to return a single column that represents the number of rows in the first table in the current database. The script should automatically choose the table that appears first alphabetically and it should exclude tables named dtproperties and sysdiagrams. Exclude system tables and views. Name the column CountOfTable
    It gives a hint: Use the Sys.tables Catalog view

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Hey, do your own homework!

    If you at least make SOME effort to write this assignment, you might find someone on the forum that will critique your answer for you.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    ...besides, why do you need "dynamic SQL" for this? Here's one without it:

    Code:
    select t0.name, CountOfTable =sum(rows)
    from sys.partitions p
    inner joinsys.tables t0
    on p.object_id= t0.object_id
    inner join(
    select top 1 * from sys.tables t
    where t.name !='dtproperties'
    and t.name !='sysdiagrams'
    order by t.name
    ) t1 on t0.object_id= t1.object_id
    group by t0.name
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

  4. #4
    Join Date
    Oct 2009
    Location
    221B Baker St.
    Posts
    486
    I suspect the reason for the exercise was to learn how to implement dynamic sql - rather than getting some particular query to work . . .

  5. #5
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    Yeah, also called "How to do things the wrong way"
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •