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 > Using a var name to create/drop database
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Using a var name to create/drop database

Réponse
 
LinkBack Outils de la discussion
Vieux 10/09/2008, 11h49   #1
lbolognini@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Using a var name to create/drop database

Hi all,

I have the following script. Why can't i use @tbname when doing drop
or create?

Thanks,
Lorenzo

--

use babel

declare @tbname varchar(500)
set @tbname = 'users'

if exists (select * from information_schema.tables where table_name =
@tbname)
begin
print 'Table ' + @tbname + ' exists, dropping'
drop table users
end

begin
print 'Table ' + @tbname + ' does not exist, creating'
create table users
(
id int primary key not null,
firstname nvarchar(255) not null,
lastname nvarchar(255) not null,
email nvarchar(255) not null,
password nvarchar(255) not null,
salt nvarchar(255) not null,
created_on datetime default getdate() not null,
updated_on timestamp
);
end
  Réponse avec citation
Vieux 10/09/2008, 12h20   #2
Tibor Karaszi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using a var name to create/drop database

That is just the way it is. If this is important to you, you can use dynamic SQL. Build the command
you want to execute in a variable and then execute the contents of the variable:
EXEC(@sql)

You might want to check out the article on dynamic SQL at sommarskog.se.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


<lbolognini@gmail.com> wrote in message
news:6e026cb2-035d-46c1-8417-14a599d07ddb@r66g2000hsg.googlegroups.com...
> Hi all,
>
> I have the following script. Why can't i use @tbname when doing drop
> or create?
>
> Thanks,
> Lorenzo
>
> --
>
> use babel
>
> declare @tbname varchar(500)
> set @tbname = 'users'
>
> if exists (select * from information_schema.tables where table_name =
> @tbname)
> begin
> print 'Table ' + @tbname + ' exists, dropping'
> drop table users
> end
>
> begin
> print 'Table ' + @tbname + ' does not exist, creating'
> create table users
> (
> id int primary key not null,
> firstname nvarchar(255) not null,
> lastname nvarchar(255) not null,
> email nvarchar(255) not null,
> password nvarchar(255) not null,
> salt nvarchar(255) not null,
> created_on datetime default getdate() not null,
> updated_on timestamp
> );
> end


  Réponse avec citation
Vieux 10/09/2008, 12h28   #3
Uri Dimant
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using a var name to create/drop database

Hi
What error do you get? I see that you drop users table and do not use a
variable.



<lbolognini@gmail.com> wrote in message
news:6e026cb2-035d-46c1-8417-14a599d07ddb@r66g2000hsg.googlegroups.com...
> Hi all,
>
> I have the following script. Why can't i use @tbname when doing drop
> or create?
>
> Thanks,
> Lorenzo
>
> --
>
> use babel
>
> declare @tbname varchar(500)
> set @tbname = 'users'
>
> if exists (select * from information_schema.tables where table_name =
> @tbname)
> begin
> print 'Table ' + @tbname + ' exists, dropping'
> drop table users
> end
>
> begin
> print 'Table ' + @tbname + ' does not exist, creating'
> create table users
> (
> id int primary key not null,
> firstname nvarchar(255) not null,
> lastname nvarchar(255) not null,
> email nvarchar(255) not null,
> password nvarchar(255) not null,
> salt nvarchar(255) not null,
> created_on datetime default getdate() not null,
> updated_on timestamp
> );
> end



  Réponse avec citation
Vieux 10/09/2008, 13h34   #4
lbolognini@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using a var name to create/drop database

On Sep 10, 12:20 pm, "Tibor Karaszi"
<tibor_please.no.email_kara...@hotmail.nomail.co m> wrote:
> That is just the way it is. If this is important to you, you can use dynamic SQL. Build the command
> you want to execute in a variable and then execute the contents of the variable:
> EXEC(@sql)
>
> You might want to check out the article on dynamic SQL at sommarskog.se.


Great, thanks a lot. I've modified the statement in this way:

declare @drop_cmd varchar(500)
set @drop_cmd = 'drop table ' + @tbname
exec(@drop_cmd)

This has the advantage that i have to change the table name only once
and it would be easier to generate scripts to automate database
creation/destruction/inserts to play around when i'm in testing.

Thanks,
Lorenzo
  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 06h03.


É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,09504 seconds with 12 queries