一些sql 語句

2021-08-22 03:46:12 字數 3322 閱讀 2702

1. 行列轉換--普通

假設有張學生成績表(cj)如下

name subject result

張三 語文 80

張三 數學 90

張三 物理 85

李四 語文 85

李四 數學 92

李四 物理 82

想變成

姓名 語文 數學 物理

張三 80 90 85

李四 85 92 82

declare @sql varchar(4000)

set @sql = 'select name'

select @sql = @sql + ',sum(case subject when '''+subject+''' then result end) ['+subject+']'

from (select distinct subject from cj) as a

select @sql = @sql+' from test group by name'

exec(@sql)

2. 行列轉換--合併

有表a,

id pid

1 11 2

1 32 1

2 23 1

如何化成表b:

id pid

1 1,2,3

2 1,2

3 1建立乙個合併的函式

create function fmerg(@id int)

returns varchar(8000)

asbegin

declare @str varchar(8000)

set @str=''

select @str=@str+','+cast(pid as varchar) from 表a where id=@id set @str=right(@str,len(@str)-1)

return(@str)

endgo

--呼叫自定義函式得到結果

select distinct id,dbo.fmerg(id) from 表a

3. 如何取得乙個資料表的所有列名

方法如下:先從systemobject系統表中取得資料表的systemid,然後再syscolumn表中取得該資料表的所有列名。

sql語句如下:

declare @objid int,@objname char(40)

set @objname = 'tablename'

select @objid = id from sysobjects where id = object_id(@objname)

select 'column_name' = name from syscolumns where id = @objid order by colid

是不是太簡單了? 呵呵 不過經常用阿.

4. 通過sql語句來更改使用者的密碼

修改別人的,需要sysadmin role

exec sp_password null, 'newpassword', 'user'

如果帳號為sa執行exec sp_password null, 'newpassword', sa

5. 怎麼判斷出乙個表的哪些欄位不允許為空?

select column_name from information_schema.columns where is_nullable='no' and table_name=tablename

6. 如何在資料庫裡找到含有相同欄位的表?

a. 查已知列名的情況

select b.name as tablename,a.name as columnname

from syscolumns a inner join sysobjects b

on a.id=b.id

and b.type='u'

and a.name='你的欄位名字'

b. 未知列名查所有在不同表出現過的列名

select o.name as tablename,s1.name as columnname

from syscolumns s1, sysobjects o

where s1.id = o.id

and o.type = 'u'

and exists (

select 1 from syscolumns s2

where s1.name = s2.name

and s1.id <> s2.id

)7. 查詢第***行資料

假設id是主鍵:

select *

from (select top *** * from yourtable) aa

where not exists(select 1 from (select top ***-1 * from yourtable) bb where aa.id=bb.id)

如果使用游標也是可以的

fetch absolute [number] from [cursor_name]

行數為絕對行數

8. sql server日期計算

a. 乙個月的第一天

select dateadd(mm, datediff(mm,0,getdate()), 0)

b. 本週的星期一

select dateadd(wk, datediff(wk,0,getdate()), 0)

c. 一年的第一天

select dateadd(yy, datediff(yy,0,getdate()), 0)

d. 季度的第一天

select dateadd(qq, datediff(qq,0,getdate()), 0)

e. 上個月的最後一天

select dateadd(ms,-3,dateadd(mm, datediff(mm,0,getdate()), 0))

f. 去年的最後一天

select dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate()), 0))

g. 本月的最後一天

select dateadd(ms,-3,dateadd(mm, datediff(m,0,getdate())+1, 0))

h. 本月的第乙個星期一

select dateadd(wk, datediff(wk,0,

dateadd(dd,6-datepart(day,getdate()),getdate())

), 0)

i. 本年的最後一天

select dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate())+1, 0))。

一些sql語句

一。在oracle中建表,怎麼實現id自動編號 1 建表 create table code test id int,name varchar2 20 2.建立序列 create sequence s country id increment by 1 start with 1 maxvalue 9...

一些Sql語句

case when xx then yy else zz 例 case when count is null then 0 else count 當count為空的時候賦值0,不為空則取原值 isnull express1,express2 例 isnull count,0 當count為空的時候則...

一些SQL語句

在工作中收集了一些有用的語句 加密 解密 declare clearpwd varchar 255 declare encryptedpwd varbinary 255 select clearpwd test select encryptedpwd convert varbinary 255 pw...