常用sql語句

2022-07-19 05:30:17 字數 2886 閱讀 6822

use master

if exists(select * from master.dbo.sysdatabases where name='mytback')

drop database mytback

gocreate database mytback

gouse mytback

goif exists(select * from sysobjects where name='tt')

drop table tt

gocreate table tt(id    int    primary key not null,

name  varchar(20)  not null)

godeclare  @i int

set @i=1

while(@i<5)

begin

insert into tt values(1001+@i,'zhangsan'+convert(char,@i))

set @i=@i+1

enddrop table tt

goif exists(select * from sysdatabases where name='skybusiness')

begin

drop database skybusiness

print 'skybusiness已經存在,已被刪除@'

endelse

begin

create database skybusiness

on primary

(name=skybusiness_mdf,

filename='c:\a\skybusiness.mdf',

size=10mb,

maxsize=50mb,

filegrowth=25%

)log on

(name=skybusiness_ldf,

filename='c:\a\skybusiness.ldf',

size=10mb,

maxsize=50mb,

filegrowth=25%

)print 'skybusiness資料庫已經建立'

end--建聚集索引

create clustered index index_yexinwinners

on tablename(column_name)

with fillfactor=10

--建非聚集索引

create nonclustered index index_yexinwinners

on tablename(column_name)

with fillfactor=10

--建檢視

create view view_name

asselect * from pubs.titles

--建檢查檢視

create view vies_name

asselect * from pubs.titles

with check option

--建加密檢視

create view view_name

with encryption

asselect * from pubs.titles

--建表

create table tablename

(stuid int not null identity(1,1) primary key,

stuname varchar(30) not null,

stuage char(3) not null

)--新增check約束

alter table tablename

add check(stuage>0 and stuage<100)

--新增外來鍵

alter table tablename

add foreign key(stuname)

references xtablename(stuname)

--新增唯一約束

alter table tablename

add unique(stuid)

--建事務

begin tran

update xtable set money=money+10000 where id=1

update xtable set money=money-10000 where id=3

if(@@error<>0)

begin

print '轉賬失敗'

rollback tran

endelse

begin

print '轉賬成功!'

commit tran

end--建游標

declare cursor_name cursor

for select * from northwind.product

---建更新游標

declare cursor_name cursor

for select * from northwind.product for update

--建隨意移動游標

declare cursor_name cursor

for select * from northwind.product

--使用游標

open cursor_name

close cursor_name

--刪除游標

deallocate cursor_name

--移動游標

fetch next--下一條記錄

fetch last--最後一條記錄

fetch first--第一條記錄

fetch prior--上一條

fetch absolute n --絕對位置

sql常用sql語句

1 查詢某個庫中所有的表名字 select name from sysobjects where xtype u and name dtproperties order by name 2 得到資料庫中所有使用者檢視 select name from sysobjects where xtype v...

常用sql語句

t sql語句複製表的方法 我在sql server 2000中有現個資料庫datahr及demo,它們的結構是一樣,其它有乙個表名為 gbitem.現在我想將demo資料庫的表名 gbitem的全部內容複製到datahr資料庫的表名為 gbitem中。請問此t sql語句應該怎麼寫?謝謝高人指點!...

常用SQL語句

查詢 sp who 中的結果值。因為儲存過程不能查詢,先轉為臨時表再查詢。declare tb table spid varchar 100 ecid varchar 100 status varchar 100 loginame varchar 100 hostname varchar 100 b...