sqlserver 常用的语句


1.更新一个字段的类型

alter table VendorForm
alter column applicationCode char(30)

2.添加/删除一个字段

if Exists( select * from syscolumns where id = OBJECT_ID('Chop_ApplicationInfo') and name = 'junctionChop')
alter table Chop_ApplicationInfo
add junctionChop char(8)

删除

alter table  TableName

drop column deleted

3.更新表中一个字段的值

  update Student

  set age =29  where Id = 2

4.更新表列名字的值

sp_rename 'DAT_Form_4_1_1_D1.Name','name','column'

5.更新表名字的值

sp_rename 'DAT_Family','DAT_FamilyOld'

6.在表中插入一行数据

insert into Student ( name, age) 

values 

('Tom', 28),

('LiLy', 25)

7.创建表

create table TableName ( 

  [Id] [int] IDENTITY(1,1) NOT NULL,
  [category] [nvarchar](128) not NULL

)

8.创建视图

create view ViewName 

as 

select

  a.Id

  b.address

from student as a

left join school as b on a.schoolid = b.id;

9.创建自定义函数

  标量值函数:

  create FUNCTION fn_week (@id int) 

  returns int 

  AS

  begin

    declare @d int

    set @d = Year(NOW()) + @id;

    return @d;

  end

  

  表值函数:

  create FUNCTION  Fun_Proc_WorkItem_AgencyApplication ( @ id  int)

  returns  table

  as

  return (

    select * from Student where id = @id

  )

10.创建存储过程

  create PROCEDURE  PRO_Update_Student  @id

  AS

  BEGIN

    update Student set deleted = 1

    where id = @id

  END

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM