Groups | Blog | Home
all groups > sql server programming > october 2006 >

sql server programming : Stored Procedure and Variable Table Names


Arnie Rowland
10/17/2006 2:58:39 PM
You will have to build a dynamic sql string and then execute it using
sp_executesql.

I recommend reading this article:

Dynamic SQL - The Curse and Blessings of Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html


--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


[quoted text, click to view]

Aaron Bertrand [SQL Server MVP]
10/17/2006 5:57:59 PM
You could pass the names as strings, however youmust use dynamic SQL to
execute.

Please read about all the pros and cons here, and protect yourself
accordingly before blindly implementing:

http://www.sommarskog.se/dynamic_sql.html




[quoted text, click to view]

David
10/17/2006 10:40:32 PM
Hello all

In my VB6 programme, I have numerous functions that retrieve data from a Jet
4.0 database.
As a some of the data retrieval is pretty much the same except for the table
and the fields that I require, I have written several generic functions to
get my data, similar to this:


Public Function Get_Data_From_A_Table(strTable as String, strField As
String, strAnotherField As String) As ADODB.Recordset
Dim strSQL as String

Open the connection etc
........................
.......................
............

strSQL = "Select " & strTable & "." & strField &" , " & strTable & "." &
strAnotherField &" _
& "FROM " & strTable &"
&" WHERE " & strTable & "." & strField &" >1"

Get the recordset etc


My question is, can I pass strTable, strField, strAnotherField into a stored
procedure in a similar fashion? How would I write it in terms of

Create Procedure Get_Data_From_A_Table (@Table_T varChar(30), @Field_F
varChar(30), AnotherField_F varChar(30))

SELECT ?? ??

Thanks for any advice

Regards

David

David
10/17/2006 11:11:59 PM
Humm Aaron, that is a very interesting article. Not too sure what to do now,
? write SP's for all my queries? I will have another think in the
morning...but that is a very good article. Thank you for your time

Regards

David

[quoted text, click to view]

David
10/17/2006 11:23:25 PM
Thanks Arnie, that is the advice of Aaron as well.
I'm not really too sure now that I want to go the dynamic SQL route. I will
have to have a think about alternatives.

Thanks for your time

Regards

David


[quoted text, click to view]

AddThis Social Bookmark Button