PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > loop statement issue
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
loop statement issue

Réponse
 
LinkBack Outils de la discussion
Vieux 16/07/2008, 16h15   #1
Mecn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut loop statement issue

hi
I am try to exec the folowing statement i got error, syntax error near
go......


DECLARE @DBName as VARCHAR(100)
declare @SQL1 varchar(800)

SELECT @dbname = min(DBName) FROM tblesafedbs
WHILE @DBName IS NOT NULL
BEGIN --loop
select @sql1 =('USE ' + @dbname + char(13) + 'Go')
exec (@sql1)
select count(*) from table1

SELECT @DBName = MIN(DBName) FROM tblesafedbs WHERE DBName > @DBName
END --loop



I CAN'T use

SET @sql1 = 'select count(*) from ' + @dbname + '.table1'


  Réponse avec citation
Vieux 16/07/2008, 17h27   #2
Tom Cooper
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: loop statement issue

go is not a SQL command. It is used by many front ends as a batch
separater, but not actually sent to SQL Server. So when in QA you write

Select Col1 From Table1
go
Select Col2 From Table2

QA does not send the go to SQL Server, instead it just sends two separate
batches to SQL Server. So you get your error because you are sending the go
to SQL Server in your exec statement. And while QA understands "go", SQL
Server doesn't and generates an error.

The second problem is that when you use a "USE" command in an exec()
command, the use of the new database is only effective why you are executing
that command. As soon as you return from the exec, you will be back to
using the original database. So you need to also put the select into the
exec().

Perhaps the following will work for you:

DECLARE @DBName as VARCHAR(100)
declare @SQL1 varchar(800)

SELECT @dbname = min(DBName) FROM tblesafedbs
WHILE @DBName IS NOT NULL
BEGIN --loop
select @sql1 =('USE ' + @dbname + ' select count(*) from table1')
exec (@sql1)


SELECT @DBName = MIN(DBName) FROM tblesafedbs WHERE DBName > @DBName
END --loop

Tom

"Mecn" <mecn@yahoo.com> wrote in message
news:%23UTJ1505IHA.3696@TK2MSFTNGP04.phx.gbl...
> hi
> I am try to exec the folowing statement i got error, syntax error near
> go......
>
>
> DECLARE @DBName as VARCHAR(100)
> declare @SQL1 varchar(800)
>
> SELECT @dbname = min(DBName) FROM tblesafedbs
> WHILE @DBName IS NOT NULL
> BEGIN --loop
> select @sql1 =('USE ' + @dbname + char(13) + 'Go')
> exec (@sql1)
> select count(*) from table1
>
> SELECT @DBName = MIN(DBName) FROM tblesafedbs WHERE DBName > @DBName
> END --loop
>
>
>
> I CAN'T use
>
> SET @sql1 = 'select count(*) from ' + @dbname + '.table1'
>



  Réponse avec citation
Vieux 17/07/2008, 15h12   #3
Rick Sawtell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: loop statement issue


"Mecn" <mecn@yahoo.com> wrote in message
news:%23UTJ1505IHA.3696@TK2MSFTNGP04.phx.gbl...
> hi
> I am try to exec the folowing statement i got error, syntax error near
> go......
>
>
> DECLARE @DBName as VARCHAR(100)
> declare @SQL1 varchar(800)
>
> SELECT @dbname = min(DBName) FROM tblesafedbs
> WHILE @DBName IS NOT NULL
> BEGIN --loop
> select @sql1 =('USE ' + @dbname + char(13) + 'Go')
> exec (@sql1)
> select count(*) from table1
>
> SELECT @DBName = MIN(DBName) FROM tblesafedbs WHERE DBName > @DBName
> END --loop
>
>
>
> I CAN'T use
>
> SET @sql1 = 'select count(*) from ' + @dbname + '.table1'
>


I agree with everything Tom said. I am wondering why you can't use the last
statement, other than it is incorrect. Have you tried:

SET @sql = 'SELECT COUNT(*) FROM ' + @dbname + '..table1' -- Notice the
two periods. This will assume your default schema in that database. Your
version of the command would fail every time.


Rick Sawtell


  Réponse avec citation
Vieux 17/07/2008, 23h51   #4
Mecn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: loop statement issue

Thanks for all responses. I will try. I can't use the last statement beacuse
I can't use variables in the statement by open cursor statment



"Rick Sawtell" <r_sawtell@nospam.hotmail.com> wrote in message
news:eewc26A6IHA.4988@TK2MSFTNGP04.phx.gbl...
>
> "Mecn" <mecn@yahoo.com> wrote in message
> news:%23UTJ1505IHA.3696@TK2MSFTNGP04.phx.gbl...
>> hi
>> I am try to exec the folowing statement i got error, syntax error near
>> go......
>>
>>
>> DECLARE @DBName as VARCHAR(100)
>> declare @SQL1 varchar(800)
>>
>> SELECT @dbname = min(DBName) FROM tblesafedbs
>> WHILE @DBName IS NOT NULL
>> BEGIN --loop
>> select @sql1 =('USE ' + @dbname + char(13) + 'Go')
>> exec (@sql1)
>> select count(*) from table1
>>
>> SELECT @DBName = MIN(DBName) FROM tblesafedbs WHERE DBName > @DBName
>> END --loop
>>
>>
>>
>> I CAN'T use
>>
>> SET @sql1 = 'select count(*) from ' + @dbname + '.table1'
>>

>
> I agree with everything Tom said. I am wondering why you can't use the
> last statement, other than it is incorrect. Have you tried:
>
> SET @sql = 'SELECT COUNT(*) FROM ' + @dbname + '..table1' -- Notice the
> two periods. This will assume your default schema in that database. Your
> version of the command would fail every time.
>
>
> Rick Sawtell
>
>



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10008 seconds with 12 queries