Visualizzazione dei risultati da 1 a 3 su 3

Discussione: MSSql

  1. #1

    MSSql

    Ciao ragazzi come al solito ci sono problemi con *ruba.

    Allora per forza di cose ho dovuto acquistare un server MSSql su *ruba per far girare meglio il mio sito .

    Quindi ho dovuto fare un import da access to MSSql .

    1) Mi sono creato la struttura delle tabelle su MSSql , però non ho messo l'identity sugli ID di alcune tabelle .

    2) mi sono fatto una pagina asp che mi importa i dati da access a MSSql , questo perchè l'importazione automatica di un file xml o csv non ha funzionato . quindi sono stato costretto a risolvermela da solo.


    Non ho messo l'identity sulle tabelle perchè volevo mantenere lo storico , poi una volta importato tutto pensavo come qualsisi MSSql , di rientrare nella struttura della tabella e mettere L'id come identity

    Con il cavolo *ruba non te lo permette più , cioè se hai creato una tabella e non setti subito l'identity successivamente non lo puoi piu fare.

    Allora ragazzi vi chiedo cortesemente esiste un sp o qualche istruzione che da codice riesco a far diventare un campo bigint : identity ?

    Grazie.
    if u are junior then Hallo!!!
    if u are senior then Cool!!!
    if u are a lady then Hi baby!!!

    http://www.solamentegratis.it

  2. #2

  3. #3

    Soluzione e funziona

    Grazie mille per la risposta , ma già avevo trovata una cosa simile ma non mi era molto chiaro il priocedimento.

    Comunque ho trovato la soluzione la pubblico può essere di aiuto a molti:

    codice:
    exec sp_SetIdentity 'tbl1',1,'cid'   ( tabella , identity (si 1 / no 0 ) , campo
     
    
    create proc sp_SetIdentity
       @tblname varchar(128) ='mytable'
       ,@set bit=0 --//0=Remove identity, 1= set identity
       ,@colname varchar(128)=NULL --//column name to set/reset identity
    as
    
       SET NOCOUNT ON
       SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
       SET ANSI_WARNINGS OFF
    
       declare @tmpcolname varchar(128)
       declare @datatype varchar(128)
       declare @tblname_bak varchar(128)
       declare @CrLf char(2)    
    
       set @tblname_bak = @tblname + replace(cast(newid() as varchar(36)),'-','')
       set @CrLf = char(13) + char(10)
    
       --//Get Identity column name if exists
       select @tmpcolname = COLUMN_NAME
       from INFORMATION_SCHEMA.COLUMNS 
       where TABLE_NAME = @tblname 
           and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
    
       --//in case of reset identity no need to supply col name
       if (@colname is null) 
           set @colname=@tmpcolname
       
       --//Get column datatype
       select @datatype=DATA_TYPE
       from INFORMATION_SCHEMA.COLUMNS 
       where TABLE_NAME = @tblname AND COLUMN_NAME=@colname
    
       --//Build column list without Identity column
       DECLARE @cols varchar(8000), @delimiter char,@cmdSql varchar(8000)
       SET @delimiter = ','
       
       SELECT @cols = COALESCE(@cols + @delimiter,'') + COLUMN_NAME 
       FROM INFORMATION_SCHEMA.COLUMNS 
       WHERE TABLE_NAME = @tblname 
           AND COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 0
           AND COLUMN_NAME<>@colname
    
       --//Set Identity
       if @set=1 begin
           if (@tmpcolname is not null) begin
               if @tmpcolname = @colname 
                   print 'Info : ' + @colname + ' is already identity column'
               else
                   print 'Error : Table can have only one identity column'
           end
           else begin
               --//This will create an emptry table with exisitng structure            
               set @cmdsql='select top 0 ' + @cols + ' into ' + @tblname_bak + ' from ' + @tblname 
               execute(@cmdsql)
    
               --//This will add identity column
               set @cmdsql= 'SET NOCOUNT ON' + @CrLf
               set @cmdsql= @cmdsql + 'Alter table ' + @tblname_bak + @CrLf + ' Add ' + @colname + ' ' + @datatype + ' identity(1,1) not null'
               execute(@cmdsql)
               --print @cmdsql
    
               --set @cmdsql='exec sp_help ' + @tblname_bak 
               --execute(@cmdsql)            
    
               --//This will copy data from source table to a backup table
               set @cmdsql= 'SET NOCOUNT ON' + @CrLf
               set @cmdsql= @cmdsql + 'SET IDENTITY_INSERT ' + @tblname_bak + ' ON' + @CrLf
               set @cmdsql= @cmdsql + 'INSERT INTO ' + @tblname_bak + '('+ @cols + ',' + @colname +')' + @CrLf 
                        + 'SELECT ' + @cols + ',' + @colname + ' FROM ' + @tblname + @CrLf
               set @cmdsql= @cmdsql + 'SET IDENTITY_INSERT ' + @tblname_bak + ' OFF'
               execute(@cmdsql)
    
               --//Finally drop old table and rename backup table
               set @cmdsql= 'drop table ' + @tblname + @CrLf 
               set @cmdsql=@cmdsql + 'exec sp_rename ' + @tblname_bak + ',' + @tblname 
               execute(@cmdsql)
               
               print @colname + ' is set to identity'
           end
       end
       else begin
           if (@tmpcolname <> @colname) begin
               print 'Error : ' + @colname + ' is not an identity column'
           end
           else begin
               --//This will create an emptry table with exisitng structure            
               set @cmdsql= 'SET NOCOUNT ON' + @CrLf
               set @cmdsql= @cmdsql + 'select top 0 ' + @cols + ' into ' + @tblname_bak + ' from ' + @tblname 
               execute(@cmdsql)
               --print @cmdsql
    
               --//This will add identity column 
               set @cmdsql= 'SET NOCOUNT ON' + @CrLf
               set @cmdsql= @cmdsql + 'Alter table ' + @tblname_bak + @CrLf + ' Add ' + @colname + ' ' + @datatype
               execute(@cmdsql)
               
               set @cmdsql= 'SET NOCOUNT ON' + @CrLf
               set @cmdsql= @cmdsql + 'INSERT INTO ' + @tblname_bak + ' ('+ @cols + ',' + @colname +')' + @CrLf 
                        + 'SELECT ' + @cols + ',' + @colname + ' FROM ' + @tblname 
               execute(@cmdsql)
               
               --//Finally drop old table and rename backup table
               set @cmdsql= 'drop table ' + @tblname + @CrLf 
               set @cmdsql=@cmdsql + 'exec sp_rename ' + @tblname_bak + ',' + @tblname 
               execute(@cmdsql)
               
               print @colname + ' is reset'
           end
       end
    go
    if u are junior then Hallo!!!
    if u are senior then Cool!!!
    if u are a lady then Hi baby!!!

    http://www.solamentegratis.it

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.