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