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 > sp_dbcmptlevel using @dbname?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
sp_dbcmptlevel using @dbname?

Réponse
 
LinkBack Outils de la discussion
Vieux 14/07/2008, 17h25   #1
Gerry Hickman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sp_dbcmptlevel using @dbname?

Hi,

I have this T-SQL which seems to work against SQL 2000 databases running on
SQL 2005.

ALTER DATABASE mydb SET SINGLE_USER;
EXEC sp_dbcmptlevel mydb, 90;
ALTER DATABASE mydb SET MULTI_USER;

My question is, how can I re-write this such that I can replace 'mydb' with
a variable such as @dbname?

I tried using EXEC() with 'dynamic SQL' but all three statements fail. The
ALTER statements give "Permission Denied" and the sp_dbcmptlevel says you
can only run it at the 'ad hoc' level.

--
Gerry Hickman
London (UK)

  Réponse avec citation
Vieux 14/07/2008, 17h57   #2
Linchi Shea
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: sp_dbcmptlevel using @dbname?

You can try the following:

declare @sql varchar(200)
declare @db sysname
set @db = 'test'

set @sql = 'ALTER DATABASE ' + @db + ' SET SINGLE_USER;'
EXEC (@sql)
EXEC sp_dbcmptlevel @db, 80;
set @sql = 'ALTER DATABASE ' + @db + ' SET MULTI_USER;'
EXEC (@sql)

If you look at the code of sp_dbcmptlevel, you'll see that it checks
@@nestlevel upfront.

Linchi

"Gerry Hickman" wrote:

> Hi,
>
> I have this T-SQL which seems to work against SQL 2000 databases running on
> SQL 2005.
>
> ALTER DATABASE mydb SET SINGLE_USER;
> EXEC sp_dbcmptlevel mydb, 90;
> ALTER DATABASE mydb SET MULTI_USER;
>
> My question is, how can I re-write this such that I can replace 'mydb' with
> a variable such as @dbname?
>
> I tried using EXEC() with 'dynamic SQL' but all three statements fail. The
> ALTER statements give "Permission Denied" and the sp_dbcmptlevel says you
> can only run it at the 'ad hoc' level.
>
> --
> Gerry Hickman
> London (UK)
>
>

  Réponse avec citation
Vieux 15/07/2008, 10h20   #3
Gerry Hickman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sp_dbcmptlevel using @dbname?

Thanks Linchi,

It's now working! You pointed out you can pass the database name direct as
the first param to sp_dbcmptlevel, no need to build a string first, that was
my first mistake. The reason the ALTER statements were not working for me
was due to a typo in the database name I was using to build the strings.

"Linchi Shea" <LinchiShea@discussions.microsoft.com> wrote in message
news:0421B58A-B839-417D-A631-F342B8F521CF@microsoft.com...
> You can try the following:
>
> declare @sql varchar(200)
> declare @db sysname
> set @db = 'test'
>
> set @sql = 'ALTER DATABASE ' + @db + ' SET SINGLE_USER;'
> EXEC (@sql)
> EXEC sp_dbcmptlevel @db, 80;
> set @sql = 'ALTER DATABASE ' + @db + ' SET MULTI_USER;'
> EXEC (@sql)
>
> If you look at the code of sp_dbcmptlevel, you'll see that it checks
> @@nestlevel upfront.
>
> Linchi
>
> "Gerry Hickman" wrote:
>
>> Hi,
>>
>> I have this T-SQL which seems to work against SQL 2000 databases running
>> on
>> SQL 2005.
>>
>> ALTER DATABASE mydb SET SINGLE_USER;
>> EXEC sp_dbcmptlevel mydb, 90;
>> ALTER DATABASE mydb SET MULTI_USER;
>>
>> My question is, how can I re-write this such that I can replace 'mydb'
>> with
>> a variable such as @dbname?
>>
>> I tried using EXEC() with 'dynamic SQL' but all three statements fail.
>> The
>> ALTER statements give "Permission Denied" and the sp_dbcmptlevel says you
>> can only run it at the 'ad hoc' level.
>>
>> --
>> Gerry Hickman
>> London (UK)
>>
>>


  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 00h54.


É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,09925 seconds with 11 queries