--数据库--------------------------------------------------
--创建库
GO
CREATE DATABASE DB1
ON
(
NAME = 'DB1',
FILENAME = 'F:/Files/Database/DB1.mdf'
)
--删除库
DROP DATABASE DB1
USE DB1
--数据表--------------------------------------------------
/*tab1、tab2、tab3*/
--创建表
GO
CREATE TABLE tabTemp
(
id int IDENTITY(1,1) NOT NULL,
username nvarchar(50),
userpassword nvarchar(50),
subject nvarchar(200),
face nvarchar(50),
sex bit,
qq nvarchar(50),
email nvarchar(50),
content nvarchar(max),
ip nvarchar(50),
writetime datetime
)
--删除表
IF EXISTS(SELECT name FROM sysobjects WHERE name='tab3') AND OBJECTPROPERTY(OBJECT_ID('tab3'),'IsUserTable')=1
DROP TABLE tab3
--重命名
SP_RENAME 'tabTemp', 'tab3'
--添加列
ALTER TABLE tab3
ADD column1 nvarchar(50)
--修改列
ALTER TABLE tab3
ALTER COLUMN column1 int
--删除列
ALTER TABLE tab3
DROP COLUMN column1
--存储过程--------------------------------------------------
/*sp_Delete、sp_Insert、sp_Select*/
--删除已存在
GO
IF EXISTS(SELECT name FROM sysobjects WHERE name='sp_Select' AND type='P')
DROP PROCEDURE sp_Select
--查询记录
GO
CREATE PROCEDURE sp_Select
@TabName nvarchar(20)
AS
BEGIN
SET NOCOUNT ON
EXECUTE('select * from ' + @TabName)
END
EXECUTE sp_Select 'tab1'
--删除已存在
GO
IF EXISTS(SELECT name FROM sysobjects WHERE name='sp_Insert' AND type='P')
DROP PROCEDURE sp_Insert
--插入记录
GO
CREATE PROCEDURE sp_Insert
@username nvarchar(50),
@userpassword nvarchar(50),
@subject nvarchar(200),
@face nvarchar(50),
@sex bit,
@qq nvarchar(50),
@email nvarchar(50),
@content nvarchar(max),
@ip nvarchar(50),
@writetime datetime,
@autoID int OUTPUT
AS
BEGIN
INSERT INTO [tab1]
([username]
,[userpassword]
,[subject]
,[face]
,[sex]
,[qq]
,[email]
,[content]
,[ip]
,[writetime])
VALUES
(@username,
@userpassword,
@subject,
@face,
@sex,
@qq,
@email,
@content,
@ip,
@writetime)
SELECT @autoID = @@IDENTITY
END
EXECUTE sp_Insert 'aaa','111','过程标题','8',1,'','[email protected]','过程内容','192.168.0.1','2000-12-25 12:00:00',0
--删除已存在
GO
IF EXISTS(SELECT name FROM sysobjects WHERE name='sp_Delete' AND type='P')
DROP PROCEDURE sp_Delete
--删除记录
GO
CREATE PROCEDURE sp_Delete
@username nvarchar(50)
AS
BEGIN
select count(*) as dc from tab1 where username = @username
delete from tab1 where username = @username
END
EXECUTE sp_Delete 'aaa'
--触发器--------------------------------------------------
sp_help 'tg_Update' --基本信息
sp_helptext 'tg_Update' --正文信息
--删除已存在
GO
IF EXISTS(SELECT name FROM sysobjects WHERE name='tg_Update' AND type = 'TR')
DROP TRIGGER tg_Update
--更新记录
GO
CREATE TRIGGER tg_Update
ON tab1
FOR update
AS
BEGIN
DECLARE @un_Deleted nvarchar(50)
DECLARE @un_Inserted nvarchar(50)
SELECT @un_Deleted = (select username FROM DELETED) --从DELETED中取更新前的值
SELECT @un_Inserted = (select username FROM INSERTED) --从INSERTED中取更新后的值
update tab2 set username=@un_Inserted where username=@un_Deleted
print @un_Deleted + ' ' + @un_Inserted
END
update tab1 set username='小红' where username='小滨'
--------------------------------------------------
--创建临时表
create table #t (
myid int not null,
mycd varchar(20) null
)
--删除临时表
declare @tmpTable nvarchar(300)
IF EXISTS(SELECT name
FROM tempdb..sysobjects
WHERE name like '#t'+'%'
AND type = 'U')
begin
set @tmpTable=''
set @tmpTable=(SELECT name FROM tempdb..sysobjects WHERE name like '#t'+'%' AND type = 'U')
exec('drop table '+@tmpTable)
end
GO
--说明
/*
临时表的命名规则:
#t 表示本地变量
##t 表示全局变量
*/