SQL Server Select into是在尝试为每个表生成动态更新语句时选择最后一条记录

6kkfgxo0  于 2022-11-21  发布在  其他
关注(0)|答案(1)|浏览(166)

我有一个如下表:

存档:

  1. TableName ColumnName Datatype NewValue
  2. Employee FirstName nvarchar Abc
  3. Employee LastName nvarchar Pqr
  4. Employee Age int 28
  5. Address City nvarchar Chicago
  6. Address StreetName nvarchar KKKKK

我正在尝试为每个表创建动态更新语句,并希望为每个单独的表执行更新语句,并在任何更新失败时进行回滚。
每个单独表的预期更新语句查询:

  1. Update Employee set FirstName = 'Abc', LastName = 'Pqr', Age = 21
  2. where DepartmentId = 100;
  3. Update Address set City = 'Chicago', StreetName = 'KKKKK'
  4. where DepartmentId = 100;

存储过程:

  1. Alter PROC [dbo].[UpdateDataByDeptID]
  2. @departmentId int
  3. As
  4. Begin
  5. Declare db_cursor CURSOR for select TableName from Archival
  6. group by TableName
  7. Declare @tableName nvarchar(50),
  8. @columnName nvarchar(50),
  9. @datatype nvarchar(50),
  10. @newValue nvarchar(50)
  11. open db_cursor
  12. Fetch Next from db_cursor into @tableName;
  13. While @@FETCH_STATUS = 0
  14. Begin
  15. select @columnName = ColumnName,
  16. @datatype = Datatype,
  17. @newValue = NewValue
  18. from Archival where TableName = @tableName;
  19. print @tableName + ' ' + @columnName + ' ' + @newValue
  20. Fetch Next from db_cursor into @tableName
  21. End;
  22. close db_cursor;
  23. DEALLOCATE db_cursor;
  24. Begin Transaction
  25. Begin Try
  26. --execute each of the update statement like below for ex:
  27. Update Employee .....
  28. Update Address .....
  29. Commit transaction
  30. End Try
  31. Begin Catch
  32. Rollback
  33. End Catch
  34. End
  35. End -- end of SP

但问题是这一行:

  1. print @tableName + ' ' + @columnName + ' ' + @newValue

上面一行给出了每个表的最后一条记录。例如,对于Employee表:

  1. Employee Age int 28

因此,我无法为每个表生成单独的更新语句。
对于我的事务,在对每个单独的表执行每个update语句后,最终的预期输出如下:

  1. Begin Transaction
  2. Begin Try
  3. --execute each of the update statement like below for ex:
  4. Update Employee set FirstName = 'Abc', LastName = 'Pqr', Age = 21
  5. where DepartmentId = 100;
  6. Update Address set City = 'Chicago', StreetName = 'KKKKK'
  7. where DepartmentId = 100;
  8. Commit transaction
  9. End Try
  10. Begin Catch
  11. Rollback
  12. End Catch
  13. End

有人能帮我吗?

fnatzsnv

fnatzsnv1#

如何 实现 这 一 点 的 一 个 示例 是 使用 动态 SQL 。
例如 :

  1. DECLARE @sql NVARCHAR(MAX) = N'';
  2. DECLARE @tablename NVARCHAR(255);
  3. DECLARE db_cursor CURSOR FOR
  4. SELECT DISTINCT [TableName] FROM [Archival];
  5. OPEN db_cursor;
  6. FETCH NEXT FROM db_cursor INTO @tablename;
  7. WHILE @@FETCH_STATUS = 0
  8. BEGIN
  9. SELECT @sql += N'UPDATE ' + QUOTENAME(@tablename) + ' SET ';
  10. SELECT @sql += QUOTENAME([ColumnName]) + N' = ' + QUOTENAME([NewValue], '''') + N', '
  11. FROM [Archival]
  12. WHERE [TableName] = @tablename;
  13. SELECT @sql = SUBSTRING(@sql, 1, LEN(@sql) - 1) + N'
  14. WHERE DepartmentId = 100;
  15. ';
  16. FETCH NEXT FROM db_cursor INTO @tablename;
  17. END
  18. CLOSE db_cursor;
  19. DEALLOCATE db_cursor;
  20. PRINT @sql;
  21. -- EXEC sp_executesql @sql;

中 的 每 一 个
我 相信 这 会 让 你 的 预期 产出 有所 提高 。
如果 您 不能 信任 NewValue 列 中 的 内容 , 那么 在 执行 此类 操作 时 , 我 会 非常 小心 。 ( 例如 , 如果 有人 可能 会 输入 某种 恶意 SQL , 并 在 此处 执行 ) 。

展开查看全部

相关问题