Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 12th, 2004, 07:05 AM
mdt01 mdt01 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 1 mdt01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question How can I use a dynamic Table name in a stored procedure

I'm sure this is really quite simple, but no matter how hard I try I can't get it to work.

I have several stored procedures that proform the same function but on different tables. I call the SP_ from ASP code. They all work fine, but it would make things easier if I could use the same SP_ and pass the relevant table name into the it.


The problem example is:

FROM tblQWIfiles WHERE Datafile = @Datafile, the same applies to the INSERT INTO line.

I want tblQWIfiles to be a variable, but I can't seem to declare it correctly and get it past the syntax checker.

Any help is much apprieciated and sorry if this really is simple.

Here is a copy of my working code:


<code>

CREATE PROCEDURE [insert_tblQWIfiles2] (
@Folder [varchar](50),
@Datafile [varchar](100),
@Issue [char](10),
@Revision [char](10),
@Owner [varchar](50),
@Extension [char](3),
@User [varchar](100)
)

AS

SET NOCOUNT ON

DECLARE @cnt int

BEGIN

SELECT @cnt = count(*)

FROM tblQWIfiles WHERE Datafile = @Datafile


IF @cnt = 0
BEGIN
INSERT INTO tblQWIfiles (
[Folder],
[Datafile],
[Issue],
[Revision],
[Owner],
[Extension],
[LastUpdated],
[LastUpdateUser]
)

VALUES
(
@Folder,
@Datafile,
@Issue,
@Revision,
@Owner,
@Extension,
Getdate(),
@User
)

SELECT 'OK' AS 'Status'
RETURN(0)
END


ELSE
BEGIN
SELECT 'ERR' AS 'Status'
RETURN(1)
END

END
GO

<code>

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > How can I use a dynamic Table name in a stored procedure


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT