Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 14
  1. #1

    Esiste un comando per SQL Server2000 tipo, "CREATE TABLE IF NOT EXISTS" di MySql

    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))"

  2. #2
    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.

  3. #3
    Perche' se esiste fai una selezione per ID ??? e se io l'ID non lo ho creato ???

  4. #4
    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...

  5. #5
    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

  6. #6
    Scusa ma che linguaggio usi???

    in VB l'accento e' un commento '
    il codice che mi hai mandato e' commentata da meta in poi

  7. #7
    Utente di HTML.it L'avatar di alexmaz
    Registrato dal
    May 2001
    Messaggi
    972
    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.

  8. #8
    Utente di HTML.it L'avatar di alexmaz
    Registrato dal
    May 2001
    Messaggi
    972
    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.

  9. #9
    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"

  10. #10
    Utente di HTML.it L'avatar di alexmaz
    Registrato dal
    May 2001
    Messaggi
    972
    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.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.