Thread: stored procedure helpppppp
-
February 15th, 2009, 01:57 PM #1Member
- Join Date
- Aug 2008
- Posts
- 54
stored procedure helpppppp
how can i write a stored procedure in which the table can be a variable?
(i mean something like "FROM @table", but this doesn't work).
help will be greatly appreciated.Last edited by Mostafa Nabil; February 16th, 2009 at 02:24 PM. Reason: wrong spelling
-
February 16th, 2009, 10:34 PM #2Not Really a Member
- Join Date
- Oct 2001
- Posts
- 27,879
you are right, you can not write the table name as a variable
SO one option is to build a string and exec the string
DECLARE @blah VARCHAR(255)
DECLARE @execstring VARCHAR(255)
SET @execstring = 'SELECT * FROM ' + @tablename + ' WHERE column = 4'
exec @execstring
if its a lengthy SP it can be very ugly and complicated, but functional
been there done that, it was very messy
-
February 17th, 2009, 09:23 AM #3Member
- Join Date
- Aug 2008
- Posts
- 54
i did it but i have a problem
when i execute it , this exception appears
(Could not find stored procedure 'SELECT Compound
FROM H1 WHERE reactant =na'.)
this is my stored procedure
ALTER PROCEDURE dbo.StoredProcedure2
(
@reactant varchar(5),
@tbl varchar(10)
)
AS
DECLARE @blah VARCHAR(255)
DECLARE @execstring VARCHAR(255)
SET @execstring = 'SELECT Compound FROM ' + @tbl + ' WHERE reactant =' + @reactant
exec @execstring
RETURN
i have a table called H1 and there is an element in reactants row called na
what's wrong?
-
February 17th, 2009, 10:36 PM #4Member
- Join Date
- Oct 2003
- Posts
- 267
try putting it in brackets
EXEC (@execstring)
-
February 18th, 2009, 12:26 AM #5Not Really a Member
- Join Date
- Oct 2001
- Posts
- 27,879
the problem will be your single quotes around what I'm assuming is a string
SO what you're doing right now is something like
select compound from tablename where reactant = somereactant
which doesn't bode well for sql
So how do you do that? well with single quotes of course!
select compound from tablename where reactant = 'somereactant'
AH but you're embedding single quotes within single quotes which breaks the string etc etc
SO the fun begins!
You want to escape all of your single quotes for the embedded string (NOT the string you're building to execute
select compound from tablename where reactant = \'somereactant\'
which should bring you some fugly looking string like
SET @execstring = 'SELECT Compound FROM ' + @tbl + ' WHERE reactant = \' ' + @reactant + '\''
simple
clear as mud?
Because these are so horrible to debug (they're worse than triggers to debug I swear... ) I'd suggest putting in some debug code
SET @execstring = 'SELECT Compound FROM ' + @tbl + ' WHERE reactant = \' ' + @reactant + '\''
PRINT @execstring
exec @execstring
so in the query analyzer you can always see exactly what you're attempting to execute.. which may not look so good until you clean it up.Last edited by vass0922; February 18th, 2009 at 12:29 AM.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Everything is being stored on D Drive
By myoung6264 in forum Technical SupportReplies: 10Last Post: January 5th, 2009, 01:28 PM -
Stored Procedure
By Jigyasa in forum Webmastering and ProgrammingReplies: 3Last Post: April 12th, 2006, 07:58 PM -
templates stored
By VERT in forum Applications and Operating SystemsReplies: 2Last Post: June 2nd, 2003, 07:50 PM -
install procedure
By Sanman in forum Applications and Operating SystemsReplies: 5Last Post: December 14th, 2002, 09:23 PM



LinkBack URL
About LinkBacks



Reply With Quote

Won a cheap audio compressor on eBay, and received a digital room relay. :confused: :heh: Seller had the room delay for auction as well, so I suspect its winner received my compressor. ;) Now...
Is It Just Me? v233893843