Results 1 to 7 of 7

Thread: [RESOLVED] can u help me for a simple quey probelm Sql server 2000

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Resolved [RESOLVED] can u help me for a simple quey probelm Sql server 2000

    Hi All,
    I am practising Sql server 2000.
    I have a table called "MetaTable" This "MetaTable" has only one field. that field name is "TableName" . and the records of the table are...

    TableName
    Student1
    Student2
    Student3

    Here the Student1,Student2 are some other table names. and all these tables will have a field called "Sname". the size of this field is 50. Now i have to write a query in such a way that i should alter the size of "sname" as 70, for all the tablenames.(means, the size of "sname" for student1, student2,student3 should be changed from 50 to 70).

    I tried a query as follows.....

    Code:
    ALTERTable (Select tableName from MetaTable) 
       ALTER COLUMN sname Varchar(70)
    I know the above statement does not work. but i hope it will give u an idea.
    If u know it, Pls help me.

    Thanks,
    raghunadhs.
    Last edited by raghunadhs; Dec 18th, 2007 at 09:43 AM. Reason: for more clarity

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: can u help me for a simple quey probelm Sql server 2000

    Code:
    ALTER TABLE Student1 MODIFY sName varchar2(70)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: can u help me for a simple quey probelm Sql server 2000

    Hi Hack,
    Thanks for your reply.
    The table names "student1", "student2"... will be available in the table called "metaTable". so i need to pick each tableName from "metaTable" and then i have to alter the size of the sname.
    SO how can i do the two things in a single query (extracting the table name from "metaTable" and then alter the size of the "sname" for the extracted tablename)?
    By using cursors i am able to alter the field size. but i am unable to do it in a single query.

    if you know.... pls let me know.....

    Thanks in advance...
    raghunadhs.


    Quote Originally Posted by Hack
    Code:
    ALTER TABLE Student1 MODIFY sName varchar2(70)
    Last edited by raghunadhs; Dec 18th, 2007 at 09:51 AM. Reason: for more clarity

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: can u help me for a simple quey probelm Sql server 2000

    Just run the query three times, once for each table.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: can u help me for a simple quey probelm Sql server 2000

    Hi Hack,
    Ok i can do it like what you have suggested. but... suppose if i don't know which tableNames are reciding in the "metatable", then how can i do it? Actually it is a re-usable query in my Stored procedure. so it should be generic.

    Quote Originally Posted by Hack
    Just run the query three times, once for each table.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: can u help me for a simple quey probelm Sql server 2000

    You cannot put a sub-query into a DDL statement like ALTER.

    You will need to run either a cursor or some other loop to get each table name and then build the ALTER string in the SPROC

    and then use EXEC(@sqlstr) to run the ALTER statement.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: can u help me for a simple quey probelm Sql server 2000

    Thanks Szlamany.
    I wrote a cursor. and it is working fine.

    regards,
    raghunadhs.

    Quote Originally Posted by szlamany
    You cannot put a sub-query into a DDL statement like ALTER.

    You will need to run either a cursor or some other loop to get each table name and then build the ALTER string in the SPROC

    and then use EXEC(@sqlstr) to run the ALTER statement.

Posting Permissions

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



Click Here to Expand Forum to Full Width