|
-
Aug 2nd, 2004, 04:07 PM
#1
Thread Starter
Fanatic Member
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:
Create procedure [dbo].[EFS_Table_Creator]
@dteYear char(4)
as
Create Table dbo.EFS_@dteYear (
pkey char(62) Null,
location char(7) null)
GO
I called this stored procedure like this.
VB Code:
rs.Open "Exec EFS_Table_Creator '2004'"
Anyone know what I did wrong?
Thanks
-
Aug 2nd, 2004, 04:44 PM
#2
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
-
Aug 2nd, 2004, 04:45 PM
#3
Thread Starter
Fanatic Member
Would you give me an example please?
Thanks
-
Aug 2nd, 2004, 04:57 PM
#4
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
-
Aug 3rd, 2004, 07:46 AM
#5
Thread Starter
Fanatic Member
All I want is an example on how to programmatically create SQL 2000 database tables using VB.
Thanks
-
Aug 3rd, 2004, 09:49 AM
#6
Fanatic Member
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
-
Aug 3rd, 2004, 03:46 PM
#7
Thread Starter
Fanatic Member
Thanks a lot vb_dba. That works perfectly. Just what I needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|