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.
| Author |
Topic |
|
kaimiddleton
Starting Member
2 Posts |
Posted - 2008-04-21 : 16:24:59
|
| I need to create a stored proc that will take the DB name and DB-user as parameters, or something like parameters, then query/update based on that. For example, these might be the first lines of my stored proc:DECLARE @dbIntData nvarchar(20)DECLARE @userIntData nvarchar(20)SET @dbIntData = '[ecity-km_IntData]'SET @userIntData = '[beiu]'DECLARE @myTable nvarchar(20)SET @myTable = @dbIntData + '.' + @userIntData + '.' + '[BE_CONTINUITY_FULFILL_HEADER]'select * from @myTableEven assuming the above would work (it doesn't, I get: Must declare the table variable "@myTable"1), I'd still like to know the answer to the following question: If this stored procedure resides within database X (in the example above it's ecity-km_IntData), how can I get that database name into a variable so I can use it? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-04-21 : 16:33:31
|
| You need to use dynamic SQL.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-04-21 : 16:36:25
|
=http://sommarskog.se/dynamic_sql.html elsasoft.org |
 |
|
|
kaimiddleton
Starting Member
2 Posts |
Posted - 2008-04-23 : 14:40:03
|
quote: Originally posted by jezemine =http://sommarskog.se/dynamic_sql.html
Thanks ... that looks like what I need. In particualar, in the section on sp_executesql http://sommarskog.se/dynamic_sql.html#sp_executesqlit says:quote: Let's look at an example. Say that in your database, many tables have a column LastUpdated, which holds the time a row last was updated. You want to be able to find out how many rows in each table that were modified at least once during a period. This is not something you run as part of the application, but something you run as a DBA from time to time, so you just keep it as a script that you have a around. Here is how it could look like:DECLARE @tbl sysname, @sql nvarchar(4000), @params nvarchar(4000), @count intDECLARE tblcur CURSOR STATIC LOCAL FOR SELECT object_name(id) FROM syscolumns WHERE name = 'LastUpdated' ORDER BY 1OPEN tblcurWHILE 1 = 1BEGIN FETCH tblcur INTO @tbl IF @@fetch_status <> 0 BREAK SELECT @sql = N' SELECT @cnt = COUNT(*) FROM dbo.' + quotename(@tbl) + N' WHERE LastUpdated BETWEEN @fromdate AND ' + N' coalesce(@todate, ''99991231'')' SELECT @params = N'@fromdate datetime, ' + N'@todate datetime = NULL, ' + N'@cnt int OUTPUT' EXEC sp_executesql @sql, @params, '20060101', @cnt = @count OUTPUT PRINT @tbl + ': ' + convert(varchar(10), @count) + ' modified rows.'ENDDEALLOCATE tblcur ...You may wonder why I do not pass @tbl as a parameter as well. The answer is that you can't. Dynamic SQL is just like any other SQL. You can't specify a table name through a variable in T-SQL, that's the whole story. Thus, when you need to specify things like table names, column names etc dynamically, you must interpolate them into the string.
|
 |
|
|
|
|
|
|
|