Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to Query a Table using Defined @Variable?

Author  Topic 

billieh
Starting Member

2 Posts

Posted - 2010-09-21 : 18:43:07

I am looking to create a Query that will produce results from a table based on the variables sent to it.

Basically, I want to be able to use the same query, but populate just the 4 variables, so no one has to edit the actual query itself.

I would like to set the variables, Show the 'before', make the change, then show the 'After' to verify it works.




The example, which doesn't work, because select can't seem to pull variables the way this is written:


DECLARE @DBName VARCHAR(50)
DECLARE @TableName VARCHAR(50)
DECLARE @ColumnName VARCHAR(50)
DECLARE @BeforeString VARCHAR(50)
DECLARE @AfterString VARCHAR(50)
DECLARE @RSLT VARCHAR(100)

Set @DBName = 'MyAddyBook'
Set @TableName = 'AllUsers'
Set @ColumnName = 'DEFAULT_EMAIL'
Set @BeforeString = '123456[at]test.com'
Set @AfterString = '654321[at]test.com'


Use @DBName
Select @ColumnName from @TableName AS 'Before Change'
Where @ColumnName like '@BeforeString'

Update @TableName
Set @ColumnName = Replace(@ColumnName, @BeforeString, @AfterString) -- this is used because there may be other comma separated information in this column.

Use @DBName
Select @ColumnName from @TableName AS 'After Change'
Where @ColumnName like '@AfterString' and date(edit_date)>=date( NOW())

Results:

Before Change
------------------------
123456[at]test.com

1 Row(s) Returns
**
Query Executed Successfully.
**
After Change
------------------------
654321[at]test.com

1 Row(s) Returns



Secondly:
Once I get this part working, I would like to have this script loop through multiple DBs on the same Server. I am guessing that there would have to be a cursor or loop of some sort to specify what the 'Use [DBNAME]' changes to...

Any help would be appreciated on these topics. Thank you so much. Google hasn't been very helpful explaining why variables can't pass in a select query.

B H

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 18:46:39
You will need dynamic SQL, which is not a good idea.

Here you go: http://www.sommarskog.se/dynamic_sql.html

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 18:47:11
If this is just an SSMS script, then use the template option in there.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

billieh
Starting Member

2 Posts

Posted - 2010-09-29 : 14:20:03
This was the answer I needed. Thank you.


quote:
Originally posted by tkizer

You will need dynamic SQL, which is not a good idea.

Here you go: http://www.sommarskog.se/dynamic_sql.html

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



B H
Go to Top of Page
   

- Advertisement -