Results 1 to 7 of 7

Thread: Create new table with variable passed to stored procedure from vb

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Create new table with variable passed to stored procedure from vb

    Hi everyone. I want to execute a stored procedure from my vb code with a string variable, and create a new table in my database and name the table based on that variable.

    I tried the following, but the table was named EFS_@dteYear

    VB Code:
    1. Create procedure [dbo].[EFS_Table_Creator]
    2. @dteYear char(4)
    3. as
    4. Create Table dbo.EFS_@dteYear (
    5. pkey char(62) Null,
    6. location char(7) null)
    7. GO

    I called this stored procedure like this.
    VB Code:
    1. rs.Open "Exec EFS_Table_Creator '2004'"
    Anyone know what I did wrong?

    Thanks
    David Wilhelm

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Simply put, it doesn't work like that.

    What you'll need to do is build your SQL dynamically into a variable, then EXECute the string. It's the only way.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Would you give me an example please?

    Thanks
    David Wilhelm

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Be sure that you want to actually do this - creating a table for each year is not always the best way to archive.

    Archiving data can be done by simply having an archive table that exactly matches the table you want to archive, but add one column to the beginning - in your case ArchiveYear.

    Then you would simply archive the data by:

    Code:
    Insert into ArchiveTable Select @dteYear,* From SourceTable

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    All I want is an example on how to programmatically create SQL 2000 database tables using VB.

    Thanks
    David Wilhelm

  6. #6
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    This is what techgnome is talking about (I think):
    Code:
    Create procedure [dbo].[EFS_Table_Creator]
    @dteYear char(4)
    as
    Declare @sql varchar(1000)
    
    Set @Sql = 'Create Table dbo.EFS_' + @dteYear + ' (pkey char (62) Null, location char(7) null)'
    
    Exec (@Sql)
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Talking

    Thanks a lot vb_dba. That works perfectly. Just what I needed.
    David Wilhelm

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