Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL触发器:新行:表格A到新列:表格B_Mysql_Sql Server_Triggers_Pivot - Fatal编程技术网

Mysql SQL触发器:新行:表格A到新列:表格B

Mysql SQL触发器:新行:表格A到新列:表格B,mysql,sql-server,triggers,pivot,Mysql,Sql Server,Triggers,Pivot,我有一个数据库和几个表。在另一个表中插入新行后,我需要在一个表中添加一列 Table A: id | Type | Category | ShortDesc | LongDesc | Active Row 1 int(11), varchar, varchar,varchar,varchar,int Row 2 Row 3 Table B: id | Row1-ShortDesc | Row2-ShortDesc | Row3-ShortDesc Row 1 int(11), tiny(1),

我有一个数据库和几个表。在另一个表中插入新行后,我需要在一个表中添加一列

Table A: id | Type | Category | ShortDesc | LongDesc | Active
Row 1 int(11), varchar, varchar,varchar,varchar,int
Row 2
Row 3

Table B: id | Row1-ShortDesc | Row2-ShortDesc | Row3-ShortDesc
Row 1 int(11), tiny(1), tiny(1), tiny(1) etc...
Row 2 
Row 3 
当我偶尔向TableA添加新行(项目)时,我希望在TableB中有一个新列。TableA是一个长期发展的集合。由于明显的遗留原因,无法删除表A中的行

因此,当我在TableA中插入一行时,我需要在TableB中插入/追加另一列

任何帮助都将不胜感激

TIA.

答案来自SQL培训 我终于能够利用俄亥俄州辛辛那提市MAX TRAINING的SQL Server类派生并创建触发器解决方案

--SQL代码 --创建一个名为TableA的表,它只保存触发器的一些数据 --此表的主键以1作为种子并递增1

CREATE TABLE TableA(
id int identity(1,1) PRIMARY KEY,
name varchar(60) NOT NULL,
shortDesc varchar(60) NOT NULL,
longDesc varchar(60) NOT NULL,
bigDesc TEXT NOT NULL
)
GO
CREATE TABLE TableB(
id int identity(1,1) PRIMARY KEY
)
GO
--创建一个只有ID列的表TableB。ID作为主键,种子为1,增量为1

CREATE TABLE TableA(
id int identity(1,1) PRIMARY KEY,
name varchar(60) NOT NULL,
shortDesc varchar(60) NOT NULL,
longDesc varchar(60) NOT NULL,
bigDesc TEXT NOT NULL
)
GO
CREATE TABLE TableB(
id int identity(1,1) PRIMARY KEY
)
GO
--只是想看看那两张桌子上什么都没有

select * from TableA
select * from TableB
GO
--表A中基于插入的实际触发器

CREATE TRIGGER TR_myInserCol
ON TableA
AFTER INSERT
AS
BEGIN
-- Don't count the trigger events
    SET NOCOUNT ON;
-- Because we are making strings we declare some variables
    DECLARE @newcol as varchar(60);
    DECLARE @lastRow as int;
    DECLARE @sql as varchar(MAX);
-- Now fill the variables
-- make sure we are looking at the last, freshly inserted row
    SET @lastRow = (SELECT COUNT(*) FROM TableA);
-- Make a SELECT statement for the last row
    SET @newcol = (SELECT shortDesc FROM TableA WHERE id = @lastRow);
-- Adds a new column in TableB is inserted based on a
-- TableA.shortDesc as the name of the new column.
-- You can use any row data you want but spaces and
-- special characters will require quotes around the field.
    SET @sql = ('ALTER TABLE TableB ADD ' + @newcol + ' char(99)');
-- And run the SQL statement as a combined string
    exec(@sql);
END;
GO
--在表格a中插入新行 --触发器将触发并在表B中添加一列

INSERT INTO TableA
(name,shortDesc,longDesc,bigDesc)
VALUES ('attract','Attraction','Attractions','Places to go see and have 
fun');
GO
INSERT INTO TableA
(name,shortDesc,longDesc,bigDesc)
VALUES ('camp','Camp','CAMP GROUND','Great place to sleep next to a creek');
GO
(name,shortDesc,longDesc,bigDesc)
VALUES ('fuel','GasStation','Fueling Depot','Get gas and go');
GO
INSERT INTO TableA
(name,shortDesc,longDesc,bigDesc)
VALUES ('petstore','PetStore','Pet Store','Get a friend');
GO
select * from TableA
select * from TableB
GO
--请参见表A中新创建的行和表B中新创建的列

INSERT INTO TableA
(name,shortDesc,longDesc,bigDesc)
VALUES ('attract','Attraction','Attractions','Places to go see and have 
fun');
GO
INSERT INTO TableA
(name,shortDesc,longDesc,bigDesc)
VALUES ('camp','Camp','CAMP GROUND','Great place to sleep next to a creek');
GO
(name,shortDesc,longDesc,bigDesc)
VALUES ('fuel','GasStation','Fueling Depot','Get gas and go');
GO
INSERT INTO TableA
(name,shortDesc,longDesc,bigDesc)
VALUES ('petstore','PetStore','Pet Store','Get a friend');
GO
select * from TableA
select * from TableB
GO
--除非要删除新创建的表,否则不要执行。 --使用此选项删除表 --清理您的工作空间,以便进行更改并重试

DROP TABLE TableA;
DROP TABLE TableB;
GO
再次感谢那些试图帮助我的人。是的,我仍然理解这可能不是最好的解决方案,但对我来说,这是可行的,因为我每年可能只会在TableA中插入几行,而且很可能在未来几年中最多只插入300行,因为我正在使用的数据不会频繁更改,并且只有一行可以使用单个位(t/F)访问现在,我可以快速将TableB分配给位置和人员,以满足他们的搜索条件,并生成一个漂亮的SQL查询字符串,而无需跨多个页面进行多次读取。再次感谢

如果有人想添加或修改我所做的,我洗耳恭听。这一切都是关于学习和分享


Michael

Micheal我认为一个表中最多可以有1024列,如果表中有1024行以上,您会怎么做。简言之,你以一种非常非常错误的方式处理这个问题。您应该在TableB中为TableA中的每一行添加一行,并在TableB中仅添加一列作为每一行的说明。MySQL或SQL Server?感谢您的回复。我应该提到,我怀疑TableA中的列数是否会超过1000列,但我预计TableB中会有数万行。我正在通过使用MySQL和phpMyAdmin我的网络主机上的cPanel。再次感谢!