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
 General SQL Server Forums
 New to SQL Server Programming
 Must declare the scalar variable

Author  Topic 

sr69
Starting Member

2 Posts

Posted - 2008-07-31 : 13:41:18
I have the code below, but it returns the following error "Must declare the scalar variable "@Dom" "
Note: The problem is when I insert the @Dom variable.

Regards

/******************************************************************/
Create table #DB
(
DomainName varchar (100),
ServerName VARCHAR(100),
DBName VARCHAR (100)
)

DECLARE @command VARCHAR(500)
DECLARE @Dom varchar(50)
set @Dom='ACME'
SET @command= 'Use [' + '?' + ']
select @Dom as DomainName, @@Servername as ServerName, ' + '''' + '?' + '''' + ' AS DBName'
INSERT INTO #DB
( DomainName,
ServerName,
DBName
)
EXEC sp_MSForEachDB @command

SELECT * FROM #DB
drop table #DB

/****************************************************************/

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-31 : 13:44:02
You cant use variables inside a sql string and use them in exec statements. the variable will be out of scope. Can i ask what you're trying to do with sp_MSForEachDB ?
Go to Top of Page

contrari4n
Starting Member

27 Posts

Posted - 2008-07-31 : 14:38:40
Is this what you are trying to do?

SELECT 'ACME' AS DomainName, @@ServerName AS ServerName, name AS DBName
FROM sys.databases


Richard Fryar
http://www.sql-server-pro.com
SQL Server Articles and Tips
Go to Top of Page

sr69
Starting Member

2 Posts

Posted - 2008-08-01 : 09:52:52
Hi it's clear for me now thanks, I'm trying to get some statistics for db growth.

Regards
sr69


quote:
Originally posted by visakh16

You cant use variables inside a sql string and use them in exec statements. the variable will be out of scope. Can i ask what you're trying to do with sp_MSForEachDB ?

Go to Top of Page
   

- Advertisement -