473,581 Members | 6,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String / Variable Problem - Dynamic Table Name

Hi

I'm grateful for any light you can shed on this!!

I've to admit, it's an unusual design but I've multiple contact tables named
e.g. i2b_ash_contact or i2b_ted_contact .
'i2b_' and '_contact' are static but the middle part is dynamic.

Storing all contacts in one table with an identifier of e.g. 'ash' or 'ted'
for each record is not possible.
Returning the value from the dynamic Query is no problem but I don't know
how to assign it to a variable.
When I try doing this it either runs into problems with evaluating the
variables or doesn't retuen anything at all e.g. if I say at the end 'Print
@AddressID'. The variable remains empty.
How can I do something like:

DECLARE
@AddressID int,
@ProgClient (varchar(10),
@Table varchar(10)

Note: @Prog is a string e.g. 'ash' or 'ted'

SET @Table = 'i2b_ + @ProgClient + '_contact

SET @AddressID = (SELECT AddressID FROM @Table WHERE ContactID = @ContactID)

Jul 20 '05 #1
2 29753
oj
DECLARE
@AddressID int,
@ProgClient (varchar(10),
@Table varchar(10)
,@sql nvarchar(1000)

SET @Table = 'i2b_' + @ProgClient + '_contact'

SET @sql='SET @AddressID = (SELECT AddressID FROM '+@Table+' WHERE ContactID
= @ContactID)'
exec sp_executesql @sql,N'@Contact ID int, @AddressID int
output',@Contac tID,@AddressID output

select @AddressID
--
-oj
http://www.rac4sql.net
"Martin Feuersteiner" <th************ @hotmail.com> wrote in message
news:bu******** **@hercules.bti nternet.com...
Hi

I'm grateful for any light you can shed on this!!

I've to admit, it's an unusual design but I've multiple contact tables named e.g. i2b_ash_contact or i2b_ted_contact .
'i2b_' and '_contact' are static but the middle part is dynamic.

Storing all contacts in one table with an identifier of e.g. 'ash' or 'ted' for each record is not possible.
Returning the value from the dynamic Query is no problem but I don't know
how to assign it to a variable.
When I try doing this it either runs into problems with evaluating the
variables or doesn't retuen anything at all e.g. if I say at the end 'Print @AddressID'. The variable remains empty.
How can I do something like:

DECLARE
@AddressID int,
@ProgClient (varchar(10),
@Table varchar(10)

Note: @Prog is a string e.g. 'ash' or 'ted'

SET @Table = 'i2b_ + @ProgClient + '_contact

SET @AddressID = (SELECT AddressID FROM @Table WHERE ContactID = @ContactID)

Jul 20 '05 #2
Thanks Bob! That does the trick!!
Have a nice day!!!
"Bob" <BR***@DLBabson .Com> wrote in message
news:09******** *************** *****@phx.gbl.. .
Provided you are passing in the @ContractID somewhere
else outside your example, your select statement only
returns one record, and the user has at least
dbdatareader access to table, I think this should do the
trick.

I'm not sure if it's the most efficient design, but it
should work:

DECLARE
@SQL varchar(1000),
@AddressID int,
@ProgClient varchar(10),
@Table varchar(100)

SET @Table = 'i2b_' + @ProgClient + '_contact'

SET @SQL = 'SELECT AddressID FROM ' + @Table + ' WHERE
ContactID = ' + CONVERT(varchar , @ContactID)

CREATE TABLE #TmpAddressID
(AddressID int)

INSERT INTO #TmpAddressID
EXEC(@SQL)

SET @AddressID = (SELECT AddressID FROM #TmpAddressID)

PRINT @AddressID

--Not really needed, but good clean up
DROP TABLE #TmpAddressID
-----Original Message-----
Hi

I'm grateful for any light you can shed on this!!

I've to admit, it's an unusual design but I've multiple

contact tables named
e.g. i2b_ash_contact or i2b_ted_contact .
'i2b_' and '_contact' are static but the middle part is

dynamic.

Storing all contacts in one table with an identifier of

e.g. 'ash' or 'ted'
for each record is not possible.
Returning the value from the dynamic Query is no problem

but I don't know
how to assign it to a variable.
When I try doing this it either runs into problems with

evaluating the
variables or doesn't retuen anything at all e.g. if I

say at the end 'Print
@AddressID'. The variable remains empty.
How can I do something like:

DECLARE
@AddressID int,
@ProgClient (varchar(10),
@Table varchar(10)

Note: @Prog is a string e.g. 'ash' or 'ted'

SET @Table = 'i2b_ + @ProgClient + '_contact

SET @AddressID = (SELECT AddressID FROM @Table WHERE

ContactID = @ContactID)

.

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10381
by: eric | last post by:
Is it possible to have part of a table name used in a CREATE statement contained in a variable? Here's what I'd like to do, although obviously the syntax of this isn't quite right or I wouldn't be here asking: DECLARE @TblPrefix char (3) SET @TblPrefix = 'tst' CREATE TABLE @TblPrefix + TestTable (col1 int) The point there is to have a...
134
7824
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you...
6
5383
by: Doohan W. | last post by:
Hi, I'm now working with DB2, and I can't find out how to execute the contents of a string Statement, without using a Java/... procedure, only using SQL statements. I know that some SQBDs such as SQL Server allows it as the exemple bellow shows : EXECUTE 'SELECT * FROM Test' Is there any way to do so with DB2 ?
1
1244
by: adit | last post by:
Is there anyway I can query on a table that is present within another variable? This is the situation: I get a table name from a column 'TBNAME' from the catalog table SYSCOLUMNS and I would like run a query on the table present in the 'TBNAME'. I've looked into reference manuals & searched this group and couldn't find answer to this. I...
13
1627
by: Ramon F Herrera | last post by:
I am writing a program that generates source code. See a snippet below. My question is about the use of that growing 'code' variable. Is it efficient? Is is recommended for this case? The code generated can grow a lot. Perhaps I should allocate a large max size in advance? TIA, -RFH
7
6578
by: bprocopio | last post by:
Please help. I'm stumped. I need to create a dynamic variable in a procedure that will be used to update a variable of the same name in a table. i.e. the name in tblAnalysisScores are WEAQScore0, WEAQScore1,WEAQScore5, MissingWEAQ0, MissingWEAQ1, MissingWEAQ5 The 0, 1, 5 are the @Interval. I am using a cursor to loop through the table. ...
2
4793
by: Joey | last post by:
I am querying a DataSet with LINQ. I am running into a problem when trying to construct my query because in the "from" clause I do not know the table name (range variable) until runtime. Possible table names might include "SomeTable1" or "SomeTable236" etc... Unfortunately when I try to use something like "from SomeTable +...
2
2017
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in the InsertName method to add the name parameter to the variable query? public class MyClass {
0
8299
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8162
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6543
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5355
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1397
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1125
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.