一点点SQL语句的用法总结

1. Select 语句中含有参数,可以使用exec( )语句执行。

2. 可以用下列方法得到字符串'1234'

     Declare  @str  Varchar(10)
     Set   @str   =  '1234'
     Print  @str    ---此时结果:1234
     Set   @str  = '''1234''' 
     Print  @str   ---此时结果:'1234'

3. 可以用下列语句修改资料表栏位:

ALTER TABLE GPDB.dbo.Branch ALTER COLUMN BranchID VARCHAR(19) NOT NULL

4. 例如:

/*********************************************************
**CreateDate:2007/06/26
**CreateUser:yangyongli
**功能描述  :修改资料库中所有栏位
**            BranchID,OLD_BranchID --> varchar(19)
**********************************************************/
    Create Table #temp(dbName Varchar(128), --资料库名称
                       dbID   int,          --资料库id
                       xtype  Char(2))      --类别 xtype = U:用户自定义
    Create Table #tempCol(colID int,                            --资料列ID  (栏位id)
                                                colName Varchar(128)) --资料列名称(栏位名称)

    Declare @strdbName Varchar(20)   ---资料库DataBase名称
    Declare @strtbName Varchar(30)    ---资料表table名称
    Declare @strExec   Varchar(255)      ---执行语句串

    --取得所有用户定义的资料库DataBase名称
    Declare cur_dbName Cursor For
       Select Name
         From Master.dbo.sysdatabases
        Where 1 = 1
          And Name <> 'master'
          And Name <> 'tempdb'
          And Name <> 'model'
          And Name <> 'msdb'
          And Name <> 'pubs'
          And Name <> 'Northwind'
          And Name <> 'ReportServer'
          And Name <> 'ReportServerTempDB'
    --打开DataBase层游标
    OPEN cur_dbName
    While 1 = 1
    Begin
        FETCH NEXT FROM cur_dbName Into
            @strdbName
        IF NOT (@@FETCH_STATUS = 0)
           Break

        Set @strExec = 'Select name dbName, id dbID, xtype From ' + @strdbName + '.dbo.sysObjects Where 1 = 1 And status > 0'
        --Print @strExec
        exec('Insert Into #temp ' + @strExec)
        --取得所有用户定义的table名称
        Declare cur_tbName Cursor For
             Select dbName
               From #temp
              Where 1 = 1
                And xtype  = 'U'

        --打开table层游标
        OPEN cur_tbName
        While 1 = 1
        Begin
            FETCH NEXT FROM cur_tbName Into
                @strtbName

            IF NOT (@@FETCH_STATUS = 0)
               Break

            --Print @strtbName

            Set @strExec = 'Select id ColID, name colName From ' + @strdbName + '.dbo.sysColumns Where ID = (Select ID From ' + @strdbName + '.dbo.sysObjects Where Name = ''' + @strtbName + ''') And (Name = ''BranchID'' Or Name = ''OLD_BranchID'')'
            Print @strExec
            exec('Insert Into #tempCol ' + @strExec)
           
            /*If Exists(Select * From #tempCol Where 1 = 1)
            Begin
                Select @strtbName tbName, * From #tempCol
            End*/
            --若存在BranchID栏位,则修改
            If Exists(Select *
                        From #tempCol
                       Where 1 = 1 And colName = 'BranchID')
            BEGIN
                --修改BranchID栏位:Varchar(6) --> Varchar(19)
                Set @strExec = 'ALTER TABLE ' + @strdbName + '.dbo.' + @strtbName + ' ALTER COLUMN BranchID VARCHAR(19) NOT NULL'
                print @strExec
                --Exec(@strExec)
            END
            --若存在OLD_BranchID栏位,则修改
            If Exists(Select *
                        From #tempCol
                       Where 1 = 1 And colName = 'OLD_BranchID')
            BEGIN
                --修改OLD_BranchID栏位:Varchar(6) --> Varchar(19)
                Set @strExec = 'ALTER TABLE ' + @strdbName + '.dbo.' + @strtbName + ' ALTER COLUMN OLD_BranchID VARCHAR(19) NOT NULL'
                print @strExec
                --Exec(@strExec)
            END
            --删除临时表#tempCol中的资料
            Delete From #tempCol
        End
        CLOSE cur_tbName
        DEALLOCATE cur_tbName
        --删除临时表#temp中的资料
        Delete From #temp
    End
    CLOSE cur_dbName
    DEALLOCATE cur_dbName

    --删除临时表
    Drop Table #temp
    Drop Table #tempCol

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章