Con VB .NET + SQL Server2000, vorrei creare una tabella solo se non esiste gia', con MySql si usa il comando cha ho scritoto qui' sotto, come si fa per SQL Server2000 ???
Dim sql As String = "CREATE TABLE IF NOT EXISTS clienti(nome varchar(30))"
Con VB .NET + SQL Server2000, vorrei creare una tabella solo se non esiste gia', con MySql si usa il comando cha ho scritoto qui' sotto, come si fa per SQL Server2000 ???
Dim sql As String = "CREATE TABLE IF NOT EXISTS clienti(nome varchar(30))"
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[nomeTabella]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
print 'esiste'
else
print 'non esiste'
GO
Prova e fammi sapere.
Perche' se esiste fai una selezione per ID ??? e se io l'ID non lo ho creato ???
No no... con quella query vai ad interrogare una tabella di sistema... tu devi solo inserire il nome della tabella al posto giusto (dove c'è il grassetto).
Fai una prova: apri il query analizer, seleziona un db e poi incolla il codice che ti ho scritto indicando prima una tabella che esiste e poi una che non esiste. Vedrai che ti scriverà "esiste" o "non esiste" a seconda dei casi. A quel punto basterà sostituire i due Print con le istruzioni che desideri.
Fammi sapere...
Mi da errore su "string" e su "select".
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[nomeTabella]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
MessageBox.Show("Esiste gia")
Else
MessageBox.Show("Va creata")
End If
Scusa ma che linguaggio usi???
in VB l'accento e' un commento '
il codice che mi hai mandato e' commentata da meta in poi
ha usato il query analyzer di SQL Server...
puoi anche fare
if exists (select * from dbo.sysobjects where xtype = 'U' and name = 'Clienti')
print 'Esiste'
else
print 'Non esiste'
questo è SQL non vb...
The individual has always had to struggle to keep from being overwhelmed by the tribe. If you try it, you will be lonely often, and sometimes frightened. But no price is too high to pay for the privilege of owning yourself.
nel tuo programma vb puoi provare una cosa del genere:
Dim sql as String = "if not exists (select * from dbo.sysobjects where xtype = 'U' and name = 'Clienti')
create table Clienti(Nome varchar(30))"
The individual has always had to struggle to keep from being overwhelmed by the tribe. If you try it, you will be lonely often, and sometimes frightened. But no price is too high to pay for the privilege of owning yourself.
OK funziona!!!
E se mi servisse la solita cosa ma per il database invece che per la tabella?
cioe': se non esiste, crea il database
come andrebbe modificata la seguente riga di codice???
Dim sql As String = "CREATE DATABASE my_database"
Devi fare la connessione indicando come db di default master e poi scrivere una cosa del genere come riga sql:
if not exists (select * from sysdatabases where name = 'my_database')
CREATE DATABASE my_database
ciao
The individual has always had to struggle to keep from being overwhelmed by the tribe. If you try it, you will be lonely often, and sometimes frightened. But no price is too high to pay for the privilege of owning yourself.