create a table using the value of a variable

  • create a table using the value of a variable

    example:

    create uspCreateTable

    @ NameTable varchar (10)

    the

    begin

    create table @ NameTable (

    id int,

    Name varchar (20))

    end

    the intention is to create a procedure that creates tables

    changing only the name

    can someone help me

    Thank you.

  • You need to use dynamic code. But that will open the possibility of SQL injection.

    http://technet.microsoft.com/en-us/library/ms188001.aspx

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • laercionb (5/14/2014)


    create a table using the value of a variable

    example:

    create uspCreateTable

    @ NameTable varchar (10)

    the

    begin

    create table @ NameTable (

    id int,

    Name varchar (20))

    end

    the intention is to create a procedure that creates tables

    changing only the name

    can someone help me

    Thank you.

    Why create a procedure to create the same table (with a different name) over and over? Why not add another column to a single table so you know which group the belongs to? That seems like a much easier solution to me. 😉

    _______________________________________________________________

    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/

  • let's see if I can explain:

    Type want to do a range of staff.

    then have a table of date where I want to create a table where the table name is the Employee ID and she get the id of the table of date and in another column adds true or false then why the table would have to be created by parameter in the procedure.

  • laercionb (5/14/2014)


    let's see if I can explain:

    Type want to do a range of staff.

    then have a table of date where I want to create a table where the table name is the Employee ID and she get the id of the table of date and in another column adds true or false then why the table would have to be created by parameter in the procedure.

    huh? That sentence doesn't make any sense at all to me.

    You will have to use dynamic sql for this as Luis suggested previously. It throws a huge red flag when somebody wants to create a stored procedure to create a table. It is generally a sign of a system design that could be streamlined.

    _______________________________________________________________

    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/

  • Sean Lange (5/15/2014)


    laercionb (5/14/2014)


    let's see if I can explain:

    Type want to do a range of staff.

    then have a table of date where I want to create a table where the table name is the Employee ID and she get the id of the table of date and in another column adds true or false then why the table would have to be created by parameter in the procedure.

    huh? That sentence doesn't make any sense at all to me.

    You will have to use dynamic sql for this as Luis suggested previously. It throws a huge red flag when somebody wants to create a stored procedure to create a table. It is generally a sign of a system design that could be streamlined.

    Glad I wasn't the only one confused by the explanation for this request.

  • sorry me

    is having words in English that use the google translator so it was pointless.

    I want to create a scale of employees

    exist words in English that an translator use .

    this table ire catch ID of table date and add in table that was created by prodedure of create table.

    the name table will o code of employees

    and then will altered an table virtual that

    will add the created table.

    if someone has a script of a scale of employees to share already solves my problem.

    thanks for listening

  • sorry me

    is having words in English that use the google translator so it was pointless.

    I want to create a scale of employees

    exist words in English that an translator use .

    this table ire catch ID of table date and add in table that was created by prodedure of create table.

    the name table will o code of employees

    and then will altered an table virtual that

    will add the created table.

    if someone has a script of a scale of employees to share already solves my problem.

    thanks for listening

  • sorry me

    is having words in English that use the google translator so it was pointless.

    I want to create a scale of employees

    exist words in English that an translator use .

    this table ire catch ID of table date and add in table that was created by prodedure of create table.

    the name table will o code of employees

    and then will altered an table virtual that

    will add the created table.

    if someone has a script of a scale of employees to share already solves my problem.

    thanks for listening

  • sorry me

    is having words in English that use the google translator so it was pointless.

    I want to create a scale of employees

    exist words in English that an translator use .

    this table ire catch ID of table date and add in table that was created by prodedure of create table.

    the name table will o code of employees

    and then will altered an table virtual that

    will add the created table.

    if someone has a script of a scale of employees to share already solves my problem.

    thanks for listening

  • laercionb (5/15/2014)


    sorry me

    is having words in English that use the google translator so it was pointless.

    I want to create a scale of employees

    exist words in English that an translator use .

    this table ire catch ID of table date and add in table that was created by prodedure of create table.

    the name table will o code of employees

    and then will altered an table virtual that

    will add the created table.

    if someone has a script of a scale of employees to share already solves my problem.

    thanks for listening

    No biggie. We clearly do not speak the same language. However we are both capable of speaking in a common language, t-sql.

    The idea of a stored procedure that receives a parameter to create a table with that name makes my skin crawl but it is absolutely possible to do. You just need to use dynamic inside your procedure to do it. How about if you give it a shot and post what you have. We can help you dial it in from there.

    _______________________________________________________________

    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/

  • I still have no idea what you are trying to accomplish. Perhaps if you show us what you are trying to accomplish by providing us with DDL (CREATE TABLE statement(s)) for an instance of a table or set of tables, some sample data (INSERT VALUE statement), and what the final results should be.

    T-SQL is a common language that we all can understand.

  • This is nothing like DDL or Sample data. The idea here is that you post create table statements and insert statements so we can recreate the table(s) in our database. Please take a few minutes and read the first link in my signature.

    Also, you don't need to use a third party website to upload documents. You can attach files directly to your post. Scroll down while you are posting and there is a button down there for attachments.

    _______________________________________________________________

    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 14 posts - 1 through 14 (of 14 total)

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