简单:
我给个例子:
/*创建每月的表及其触发器*/
CREATE PROCEDURE NEW_SP_STATION_DM_Create_MonthTable_Exit(@StationID Varchar(6),@TableYearMonth Varchar(6)) AS
Declare @TableSQL Varchar(6500)
BEGIN /*1.生成该月的出口表*/
Select @TableSQL='CREATE TABLE [dbo].[Exit'+@TableYearMonth+@StationID+'] ('+
'[CardNetWork] [smallint] NOT NULL ,[CardID] [int] NOT NULL ,[EntryNetWork] [smallint] NOT NULL ,'+
'[EntryStation] [int] NOT NULL ,[EntryLane] [tinyint] NOT NULL ,[EntryTime] [datetime] NOT NULL ,'+
'[EntryDate] [tinyint] NOT NULL ,[EntryOperator] [int] NOT NULL ,[EntryShift] [tinyint] NOT NULL ,'+
'[RVehicleClass] [tinyint] NOT NULL ,[RDealStatus] [smallint] NOT NULL ,[ExitNetWork] [smallint] NOT NULL ,'+
'[ExitStation] [int] NOT NULL ,[ExitLane] [tinyint] NOT NULL ,[ExitTime] [datetime] NOT NULL ,'+
'[ExitDate] [tinyint] NOT NULL ,[ExitOperator] [int] NOT NULL ,[ExitShift] [tinyint] NOT NULL ,'+
'[VehicleClass] [tinyint] NOT NULL ,[PVehicleClass] [tinyint] NOT NULL ,[TotalToll] [smallint] NOT NULL ,'+
'[RealToll] [smallint] NOT NULL ,[DealStatus] [smallint] NOT NULL ,[DeviceStatus] [smallint] NOT NULL ,'+
'[OwnerNetwork] [smallint] NOT NULL ,[OwnerID] [tinyint] NOT NULL ,[OwnerToll] [smallint] NOT NULL ,'+
'[RecordType] [tinyint] NOT NULL ,[FlagStations] [int] NOT NULL ,[RODSpecification] [tinyint] NOT NULL ,'+
'[JudgeType] [tinyint] NOT NULL ,[JudgeOperator] [int] NOT NULL ,[EntryCarNumber] [varchar] (10) NOT NULL ,'+
'[ExitCarNumber] [varchar] (10) NOT NULL ,[Reserved1] [smallint] NOT NULL ,[Reserved2] [smallint] NOT NULL ,'+
'CONSTRAINT [PK_Exit'+@TableYearMonth+@StationID+'] PRIMARY KEY CLUSTERED'+
'([ExitNetWork],[ExitStation],[ExitLane],[ExitTime])'+
')ON [PRIMARY]' Exec (@TableSQL)
我给个例子:
/*创建每月的表及其触发器*/
CREATE PROCEDURE NEW_SP_STATION_DM_Create_MonthTable_Exit(@StationID Varchar(6),@TableYearMonth Varchar(6)) AS
Declare @TableSQL Varchar(6500)
BEGIN /*1.生成该月的出口表*/
Select @TableSQL='CREATE TABLE [dbo].[Exit'+@TableYearMonth+@StationID+'] ('+
'[CardNetWork] [smallint] NOT NULL ,[CardID] [int] NOT NULL ,[EntryNetWork] [smallint] NOT NULL ,'+
'[EntryStation] [int] NOT NULL ,[EntryLane] [tinyint] NOT NULL ,[EntryTime] [datetime] NOT NULL ,'+
'[EntryDate] [tinyint] NOT NULL ,[EntryOperator] [int] NOT NULL ,[EntryShift] [tinyint] NOT NULL ,'+
'[RVehicleClass] [tinyint] NOT NULL ,[RDealStatus] [smallint] NOT NULL ,[ExitNetWork] [smallint] NOT NULL ,'+
'[ExitStation] [int] NOT NULL ,[ExitLane] [tinyint] NOT NULL ,[ExitTime] [datetime] NOT NULL ,'+
'[ExitDate] [tinyint] NOT NULL ,[ExitOperator] [int] NOT NULL ,[ExitShift] [tinyint] NOT NULL ,'+
'[VehicleClass] [tinyint] NOT NULL ,[PVehicleClass] [tinyint] NOT NULL ,[TotalToll] [smallint] NOT NULL ,'+
'[RealToll] [smallint] NOT NULL ,[DealStatus] [smallint] NOT NULL ,[DeviceStatus] [smallint] NOT NULL ,'+
'[OwnerNetwork] [smallint] NOT NULL ,[OwnerID] [tinyint] NOT NULL ,[OwnerToll] [smallint] NOT NULL ,'+
'[RecordType] [tinyint] NOT NULL ,[FlagStations] [int] NOT NULL ,[RODSpecification] [tinyint] NOT NULL ,'+
'[JudgeType] [tinyint] NOT NULL ,[JudgeOperator] [int] NOT NULL ,[EntryCarNumber] [varchar] (10) NOT NULL ,'+
'[ExitCarNumber] [varchar] (10) NOT NULL ,[Reserved1] [smallint] NOT NULL ,[Reserved2] [smallint] NOT NULL ,'+
'CONSTRAINT [PK_Exit'+@TableYearMonth+@StationID+'] PRIMARY KEY CLUSTERED'+
'([ExitNetWork],[ExitStation],[ExitLane],[ExitTime])'+
')ON [PRIMARY]' Exec (@TableSQL)
解决方案 »
- 很简单的一个查询根据A表的typeid查询B表的type
- 关于字符串转为日期的一个查询问题
- sql server 2000 完整性约束问题? 请大虾帮忙给看看啊!
- 求一SQL语句
- 在sql 语句中,in 该怎么用啊?就是在用游标的时候,declare cursor for,语句中有 in 该怎么写啊?
- 存储进程不能访问!
- 关于select中的变量
- 下面这条Sql语句该怎样写呢?( 注意:自动增长的主键ID是不连续的。)
- 請教: 如何用程序代碼對sql server進行備份與還原?
- select有返回指定记录数的语法吗?
- 为什么我的DELETE语句这么慢?在线求救?
- 请问视图和索引分别是做什么用的??
SET @TableSQL='CREATE TRIGGER [Trigger_Exit'+@TableYearMonth+@StationID+'_Insert] ON [dbo].[Exit'+@TableYearMonth+@StationID+']'+Char(13)+'FOR INSERT '+Char(13)+
'AS '+Char(13)+
'BEGIN '+Char(13)+
'Declare @Lane Tinyint,'+Char(13)+
' @Operator Int,'+Char(13)+
' @PassTime DateTime,'+Char(13)+
' @OldPassTime DateTime,'+Char(13)+
' @DealStatus Smallint,'+Char(13)+
' @RecordType Tinyint,'+Char(13)+
' @TotalCars Tinyint,'+Char(13)+
' @Cash Tinyint,'+Char(13)+
' @Unpaid Tinyint,'+Char(13)+
' @Office Tinyint,'+Char(13)+
' @ArmyPolice Tinyint,'+Char(13)+
' @Emer Tinyint,'+Char(13)+
' @FreePay Tinyint,'+Char(13)+
' @Team Tinyint,'+Char(13)+
' @Unmeet Tinyint,'+Char(13)+
' @NoCard Tinyint,'+Char(13)+
' @BadCard Tinyint,'+Char(13)+
' @UTurn Tinyint,'+Char(13)+
' @OutTime Tinyint,'+Char(13)+
' @PathBreak Tinyint,'+Char(13)+
' @BlackList Tinyint,'+Char(13)+
' @GrayList Tinyint,'+Char(13)+
' @Correct Tinyint,'+Char(13)+
' @UnmeetCarNum Tinyint,'+Char(13)+
' @Simulate Tinyint,'+Char(13)+
' @UFree Tinyint,'+Char(13)+
' @Fine Tinyint'+Char(13)+Char(13)+
' @TotalCars=0,@Cash=0,@Unpaid=0,@Office=0,@ArmyPolice=0,@Emer=0,@FreePay=0,@Team=0,@Unmeet=0,@NoCard=0,@BadCard=0,'+Char(13)+
' @UTurn=0,@OutTime=0,@PathBreak=0,@BlackList=0,@GrayList=0,@Correct=0,@UnmeetCarNum=0,@Simulate=0,@UFree=0,@Fine=0'+Char(13)+
' From INSERTED'+Char(13)+
' Select @OldPassTime=PassTime From Liveshow Where Lane=@Lane'+Char(13)+ Char(13)+
' /*TotalCars(Is Traffic and Not Fine)*/'+Char(13)+
' If (@RecordType & 0x1)=0x1 AND (@DealStatus & 0x1)=0'+Char(13)+
' Select @TotalCars=@TotalCars+1'+Char(13)+ Char(13)+
' /*Cash Car*/'+Char(13)+
' If (@DealStatus & 0xFFFF)=0'+Char(13)+
' Select @Cash=@Cash+1'+Char(13)+ Char(13)+
' /*Unpaid Car*/'+Char(13)+
' If (@DealStatus & 0xF)=0x4'+Char(13)+
' Select @Unpaid=@Unpaid+1'+Char(13)+ Char(13)+
' /*Office Car*/'+Char(13)+
' If (@DealStatus & 0xF)=0x6'+Char(13)+
' Select @Office=@Office+1'+Char(13)+ Char(13)+
' /*ArmyPolice Car*/'+Char(13)+
' If (@DealStatus & 0xF)=0x8'+Char(13)+
' Select @ArmyPolice=@ArmyPolice+1'+Char(13)+ Char(13)+
' /*Emer Car*/'+Char(13)+
' If (@DealStatus & 0xF)=0xA'+Char(13)+
' Select @Emer=@Emer+1'+Char(13)+ Char(13)+
' /*FreePay Car*/'+Char(13)+
' If (@DealStatus & 0xF)=0xC'+Char(13)+
' Select @FreePay=@FreePay+1'+Char(13)+ Char(13)+
' /*Team Car*/'+Char(13)+
' If (@DealStatus & 0xF)=0xE'+Char(13)+
' Select @Team=@Team+1'+Char(13)+ Char(13)+
' /*Unmeet Car*/'+Char(13)+
' If (@DealStatus & 0x10)=0x10 '+Char(13)+
' Select @Unmeet=@Unmeet+1'+Char(13)+ Char(13)+
' /*NoCard Car*/'+Char(13)+
' If (@DealStatus & 0x20)=0x20 '+Char(13)+
' Select @NoCard=@NoCard+1'+Char(13)+ Char(13)+
' /*UTurn Car*/'+Char(13)+
' If (@DealStatus & 0x40)=0x40'+Char(13)+
' Select @UTurn=@UTurn+1'+Char(13)+ Char(13)+
' /*OutTime Car*/'+Char(13)+
' If (@DealStatus & 0x80)=0x80 '+Char(13)+
' Select @OutTime=@OutTime+1'+Char(13)+ Char(13)+
' /*PathBreak Car*/'+Char(13)+
' If (@DealStatus & 0x100)=0x100 '+Char(13)+
' Select @PathBreak=@PathBreak+1'+Char(13)+ Char(13)+
' /*BlackList Car*/'+Char(13)+
' If (@DealStatus & 0x200)=0x200'+Char(13)+
' Select @BlackList=@BlackList+1'+Char(13)+ Char(13)+
' /*GrayList Car*/'+Char(13)+
' If (@DealStatus & 0x400)=0x400'+Char(13)+
' Select @GrayList=@GrayList+1'+Char(13)+ Char(13)+
' /*BadCard Car*/'+Char(13)+
' If (@DealStatus & 0x800)=0x800'+Char(13)+
' Select @BadCard=@BadCard+1'+Char(13)+ Char(13)+
' /*Correct Car*/'+Char(13)+
' If (@DealStatus & 0x1000)=0x1000'+Char(13)+
' Select @Correct=@Correct+1'+Char(13)+ Char(13)+
' /*UnmeetCarNum Car*/'+Char(13)+
' If (@DealStatus & 0x2000)=0x2000'+Char(13)+
' Select @UnmeetCarNum=@UnmeetCarNum+1'+Char(13)+ Char(13)+
' /*Simulate Car*/'+Char(13)+
' If (@DealStatus & 0x4000)=0x4000'+Char(13)+
' Select @Simulate=@Simulate+1'+Char(13)+ Char(13)+
' /*UFree Car*/'+Char(13)+
' If (@DealStatus & 0x8000)=0x8000'+Char(13)+
' Select @UFree=@UFree+1'+Char(13)+ Char(13)+
' /*Fine Car*/'+Char(13)+
' If (@DealStatus & 0x1)=0x1'+Char(13)+
' Select @Fine=@Fine+1'+Char(13)+ Char(13)+
' /*Update Liveshow*/'+Char(13)+
' If DateDiff(Second,@OldPassTime,@PassTime)>0'+Char(13)+
CREATE TRIGGER [table_aaa_config_on_insert] ON [table_aaa_config]
FOR INSERT
ASDeclare @id int
Select @id = [id]
from inserted--添加config日志
Insert into table_aaa_config_log (keys,type,webshow,value,notes,createtime,feevalue)
Select keys,type,webshow,value,notes,createtime,feevalue
from table_aaa_config
where [id] = @idDeclare @keys varchar (20),
@servicetype varchar (10),
@feevalue varchar (6)Select @keys = keys,
@servicetype = '1',
@feevalue = feevalue
from table_aaa_config
where [id] = @idInsert Into sgip_fee (CmdCode,servicetype,feetype,feevalue,givenvalue,flag)
Values (@keys,@servicetype,'2',@feevalue,'0','0')
[/code]
@新表名 varchar(100)
as
begin
exec('select * into '+@新表名+' from table_aaa')
exec('
CREATE TRIGGER ['+@新表名+'_config_on_insert] ON ['+@新表名+']
FOR INSERT
ASDeclare @id int
Select @id = [id]
from inserted--添加config日志
Insert into table_aaa_config_log (keys,type,webshow,value,notes,createtime,feevalue)
Select keys,type,webshow,value,notes,createtime,feevalue
from table_aaa_config
where [id] = @idDeclare @keys varchar (20),
@servicetype varchar (10),
@feevalue varchar (6)Select @keys = keys,
@servicetype = ''1'',
@feevalue = feevalue
from table_aaa_config
where [id] = @idInsert Into sgip_fee (CmdCode,servicetype,feetype,feevalue,givenvalue,flag)
Values (@keys,@servicetype,''2'',@feevalue,''0'',''0'')
')
end