Home > database >  Create a trigger is used to realize: when in the main table according to the main table id is automa
Create a trigger is used to realize: when in the main table according to the main table id is automa

Time:04-28

Write a trigger dynamically generated table, the measured, produces no need to watch, which place have problem of the great god give directions

USE [ecology9]
GO
/* * * * * * Object: the Trigger [dbo] [triggAddMachineInfo] Script Date: Tuesday 2021/4/27 15:44:13 * * * * * */
The SET ANSI_NULLS ON
GO
The SET QUOTED_IDENTIFIER ON
GO
-=============================================
Author: & lt; The Author, Name>
- the Create date: & lt; The Create Date,, & gt;
- Description: & lt; The Description, & gt;
-=============================================
ALTER the TRIGGER [dbo] [triggAddMachineInfo]
ON [dbo] [uf_MachineInfo]
AFTER INSERT
AS
The BEGIN
- SET NOCOUNT ON added to prevent extra result sets from
- interfering with the SELECT statements.
SET NOCOUNT ON;

Declare @ projid varchar (40), @ tablename varchar (100), @ MachineNumber varchar (Max), @ MachineNumber2 varchar (Max), @ MachineNumber_id2 varchar (100)
Select @ projid=projid, @ MachineNumber=MachineNumber from inserted

If (ISNULL (@ projid, ' ') & lt;> "')
The begin
Select @ tablename='[realnamedata]. Dbo. LW_Checkin_data_' + @ projid
Declare @ SQL varchar (Max)

If (OBJECT_ID (@ tablename) is null)
The begin
Select SQL='create table' + @ @ tablename
Select SQL=@ @ SQL + '([itmid]/int IDENTITY (1, 1) NOT NULL, primary key,
[uuid] [varchar] (40) NULL,
[deviceId] [nvarchar] (40) NULL,
[deviceName] [nvarchar] (200), NULL,
[the ProjectName] [nvarchar] (200), NULL,
(UIO) (int) NULL,
[passtime] [datetime] NULL,
[name] [nvarchar] (100), NULL,
[gender] [nvarchar] (20) NULL,
[department] [nvarchar] (100), NULL,
[idcard] [varchar] (40) NULL,
[so] [varchar] (40) NULL,
[address] [nvarchar] (100), NULL,
[temperature] a decimal null,
Null wearmask nvarchar (40),
[lat] [varchar] (20) NULL,
[LNG] [varchar] (20) NULL,
[location] [nvarchar] (200), NULL,
[addtime] [datetime] NULL,
[ImgData] [varchar] (200) NULL) '
Select SQL + SQL=@ @ 'the CREATE INDEX index_LW_Checkin_Data_' + @ projid + '_uuid ON [lw_checkin]. Dbo. LW_Checkin_Data_' + @ projid + '(uuid)
Select SQL + SQL=@ @ 'the CREATE INDEX index_LW_Checkin_Data_' + @ projid + '_idcard ON [lw_checkin]. Dbo. LW_Checkin_Data_' + @ projid + '(idcard)
Select SQL + SQL=@ @ 'the CREATE INDEX index_LW_Checkin_Data_' + @ projid + '_uio ON [lw_checkin]. Dbo. LW_Checkin_Data_' + @ projid + '(uio)
The exec (@ SQL)
End
End
END

CodePudding user response:

An error, I guess you behind the create table to add a space

CodePudding user response:

You is more than a single insert data, including projid data is empty, just took the assignment data, lead to not go into the IF

If it is to build table statements splicing error led to build tabulated fault, didn't throw out wrong?
  • Related