查询Table name, Column name, 拼接执行sql文本, 游标, 存储过程, 临时表
018_Proc_UpdateTranslations.sql:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO if (exists (select * from sys.objects where name = 'UpdateTranslationsForTable'))
drop proc [UpdateTranslationsForTable]
go CREATE PROCEDURE [dbo].[UpdateTranslationsForTable]
(
@tableName NVARCHAR(128)
)
AS
BEGIN
-- ---
SET NOCOUNT ON
-- ---
-- ---
DECLARE @translationTableName NVARCHAR(128)
DECLARE @sql NVARCHAR(4000) -- ---
SET @tableName = REPLACE(@tableName, '''', '''''') -- injection protection
IF @tableName = '_dynamicText'
BEGIN
SET @translationTableName = @tableName
END
ELSE
BEGIN
SET @translationTableName = @tableName + 'Translation'
END -- ---------------------------------------------------------------------------
-- --- Build strings of columns that we will insert into / select from
-- --- Certain columns are NOT to be translated and those are listed here.
-- ---
-- ---------------------------------------------------------------------------
DECLARE @colTbl TABLE (rowId INT NOT NULL IDENTITY(1,1), columnName NVARCHAR(128), maxLen INT NULL, appendQ TINYINT NOT NULL, PRIMARY KEY (rowId))
-- ---
INSERT INTO @colTbl (columnName, maxLen, appendQ)
SELECT column_name, character_maximum_length,
CASE WHEN data_type = 'nvarchar' AND (character_maximum_length >= 100 OR character_maximum_length = -1)
AND column_name NOT IN ('OwnerUserId', 'UserName', 'UserId', 'OwnerUserId', 'AssessingUserName',
'CompletedById', 'TargetAttractionRationale', 'ResourceFileName', 'ResourceURL')
THEN 1 ELSE 0 END -- this case value indicates to only append ?xx- to certain columns, not these ones listed
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = @translationTableName
AND column_name NOT IN ('Language', 'Id', 'tVersion','IsSynchronized')
ORDER BY ordinal_position
-- ---
DECLARE @update NVARCHAR(4000)
DECLARE @colName NVARCHAR(128)
DECLARE @conditions NVARCHAR(128)
DECLARE @appendQ TINYINT
DECLARE @colLen NVARCHAR(20) -- for nvarchar columns, this is the column size number converted to nvarchar
DECLARE @minRow INT
DECLARE @maxRow INT
SELECT @minRow = MIN(rowId), @maxRow = MAX(rowId) FROM @colTbl
WHILE (@minRow <= @maxRow)
BEGIN
SELECT @colName = columnName, @appendQ = appendQ, @colLen = CAST(CASE WHEN maxLen = -1 THEN 4000 ELSE maxLen END AS VARCHAR) FROM @colTbl WHERE rowId = @minRow SET @update = ISNULL(@update + ', ', '') + '[' + @colName + ']='+ CASE WHEN @appendQ = 1 THEN 'SUBSTRING([' + @colName + '], 5, ' + @colLen + ')' ELSE @colName END
SET @conditions = ISNULL(@conditions + 'and ', '') + '[' + @colName + '] like ''?en-%'' ' SET @minRow = @minRow + 1
END
-- ---------------------------------------------------------------------------
-- --- update certain languages in the translation table
------------------------------------------------------------------------------ SET @sql = N'UPDATE ' + @translationTableName + ' SET '+@update+' where language like ''en-%'' and '+@conditions EXEC sp_executesql @statement = @sql END
019_Cur_UpdateTranslations.sql :
-- ===================================================================================
-- === Script to update translations for specified languages to be the same as xx-XX in all translations tables
-- ===================================================================================
DECLARE @sql NVARCHAR(4000),@tableName NVARCHAR(128)
-- ===
PRINT '--============ Starting Update for All Translations ============--'
-- ===================================================================================
-- === create a cursor for each sql table that holds translated data
-- ===================================================================================
DECLARE tableCursor CURSOR FOR
-- Select all tables that follow the 'standard' translations system
SELECT T.name
FROM sys.tables T
WHERE EXISTS (SELECT NULL FROM sys.tables TT WHERE TT.name = T.name + 'Translation')
AND T.name NOT IN ('Permission')
-- Add any tables that follow the 'standard' translations system
UNION SELECT '_dynamicText' OPEN tableCursor
FETCH tableCursor INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT ''
PRINT '------ Updating Translations For ' + @tableName + ' ------' EXEC UpdateTranslationsForTable @tableName PRINT '------ Update Translations For ' + @tableName + ' Done ------' FETCH tableCursor INTO @tableName
END
CLOSE tableCursor
DEALLOCATE tableCursor
PRINT '--============ Finished Updating All Translations ============--'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO if (exists (select * from sys.objects where name = 'UpdateTranslationsForTable'))
drop proc [UpdateTranslationsForTable]
go CREATE PROCEDURE [dbo].[UpdateTranslationsForTable]
(
@tableName NVARCHAR(128)
)
AS
BEGIN
-- ---
SET NOCOUNT ON
-- ---
-- ---
DECLARE @translationTableName NVARCHAR(128)
DECLARE @sql NVARCHAR(4000) -- ---
SET @tableName = REPLACE(@tableName, '''', '''''') -- injection protection
IF @tableName = '_dynamicText'
BEGIN
SET @translationTableName = @tableName
END
ELSE
BEGIN
SET @translationTableName = @tableName + 'Translation'
END -- ---------------------------------------------------------------------------
-- --- Build strings of columns that we will insert into / select from
-- --- Certain columns are NOT to be translated and those are listed here.
-- ---
-- ---------------------------------------------------------------------------
DECLARE @colTbl TABLE (rowId INT NOT NULL IDENTITY(1,1), columnName NVARCHAR(128), maxLen INT NULL, appendQ TINYINT NOT NULL, PRIMARY KEY (rowId))
-- ---
INSERT INTO @colTbl (columnName, maxLen, appendQ)
SELECT column_name, character_maximum_length,
CASE WHEN data_type = 'nvarchar' AND (character_maximum_length >= 100 OR character_maximum_length = -1)
AND column_name NOT IN ('OwnerUserId', 'UserName', 'UserId', 'OwnerUserId', 'AssessingUserName',
'CompletedById', 'TargetAttractionRationale', 'ResourceFileName', 'ResourceURL')
THEN 1 ELSE 0 END -- this case value indicates to only append ?xx- to certain columns, not these ones listed
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = @translationTableName
AND column_name NOT IN ('Language', 'Id', 'tVersion','IsSynchronized')
ORDER BY ordinal_position
-- ---
DECLARE @update NVARCHAR(4000)
DECLARE @colName NVARCHAR(128)
DECLARE @conditions NVARCHAR(128)
DECLARE @appendQ TINYINT
DECLARE @colLen NVARCHAR(20) -- for nvarchar columns, this is the column size number converted to nvarchar
DECLARE @minRow INT
DECLARE @maxRow INT
SELECT @minRow = MIN(rowId), @maxRow = MAX(rowId) FROM @colTbl
WHILE (@minRow <= @maxRow)
BEGIN
SELECT @colName = columnName, @appendQ = appendQ, @colLen = CAST(CASE WHEN maxLen = -1 THEN 4000 ELSE maxLen END AS VARCHAR) FROM @colTbl WHERE rowId = @minRow SET @update = ISNULL(@update + ', ', '') + '[' + @colName + ']='+ CASE WHEN @appendQ = 1 THEN 'SUBSTRING([' + @colName + '], 5, ' + @colLen + ')' ELSE @colName END
SET @conditions = ISNULL(@conditions + 'and ', '') + '[' + @colName + '] like ''?en-%'' ' SET @minRow = @minRow + 1
END
-- ---------------------------------------------------------------------------
-- --- update certain languages in the translation table
------------------------------------------------------------------------------ SET @sql = N'UPDATE ' + @translationTableName + ' SET '+@update+' where language like ''en-%'' and '+@conditions EXEC sp_executesql @statement = @sql END
查询Table name, Column name, 拼接执行sql文本, 游标, 存储过程, 临时表的更多相关文章
- Entity Framework Core 执行SQL语句和存储过程
无论ORM有多么强大,总会出现一些特殊的情况,它无法满足我们的要求.在这篇文章中,我们介绍几种执行SQL的方法. 表结构 在具体内容开始之前,我们先简单说明一下要使用的表结构. public clas ...
- EF Core 执行SQL语句和存储过程
无论ORM有多么强大,总会出现一些特殊的情况,它无法满足我们的要求.在这篇文章中,我们介绍几种执行SQL的方法. 表结构 在具体内容开始之前,我们先简单说明一下要使用的表结构. public clas ...
- Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器
---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...
- 如何用VS EF连接 Mysql,以及执行SQL语句 和存储过程?
VS2013, MySQL5.7.18 , MySQL5.7.14 执行SQL语句: ztp_user z = new ztp_user(); object[] obj = new object[] ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(89)-EF执行SQL语句与存储过程
这一节,我们来看看EF如何执行SQL语句与读取存储过程的数据,可能有一部分人,还不知道EF如何执行存储过程与原生SQL语句! 我们什么时候要直接使用原生的SQL语句? 返回值过于复杂 过于复杂的联合查 ...
- SQL内部拼接执行SQL语句时,实现变量参数化
exec sp_ExecuteSql执行的SQL语句拼接起是比较麻烦,如果关联的表多拼接过程是很容易出错的,下面这方法非常的好用,而且简单直观 if exists(select * from syso ...
- 测试JdbcTemplate执行SQL语句和存储过程
我在项目中需要使用到oracle的语句片段和存储过程.下面就是我的测试案例: public class DbTest extends BaseTestCase { @Resource JdbcUtil ...
- sql循环-游标、临时表、表变量
游标 在游标逐行处理过程中,当需要处理的记录数较大,而且游标处理位于数据库事务内时,速度非常慢. -- 声明变量 DECLARE @Id AS Int -- 声明游标 DECLARE C_Id CUR ...
- Java下拼接执行动态SQL语句(转)
在实际业务中经常需要拼接动态SQL来完成复杂数据计算,网上各类技术论坛都有讨论,比如下面这些问题: http://bbs.csdn.net/topics/390876591 http://bbs.cs ...
随机推荐
- HighCharts 根据spline-plot-bands图,定制自己的图(区间里显示多个数据)
公司项目里有这样一个需求,根据数据绘图,但是数据很多,不可能每个点每个点的去画,这样显示的数据太密集非常的难看(更显得技术不专业),如图: 所以我和项目经理商量如何显示这个图形,按照他的意思是,按照范 ...
- bzoj 1318: [Spoj744] Longest Permutation 智商题
1318: [Spoj744] Longest Permutation Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 361 Solved: 215 ...
- 在js中获取easyui datagrid的数据
可以在页面对datagrid的数据直接进行修改,然后提交到数据库,但是要求在提交前获取datagrid的所有行的数据.API提供了getData方法,但是怎么用了,没说. 最后这样写才搞定 var a ...
- DX 的.x 文件
template Header { <3D82AB43-62DA-11cf-AB39-0020AF71E433> WORD major; WORD minor; DWORD flags;} ...
- spoj 297
就是对距离进行二分找最大值 .... #include <cstring> #include <cstdio> #include <algorithm> #incl ...
- HDU 1885 Key Task(三维BFS)
题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...
- linux查找某个文件中单词出现的次数
文件名称:list 查找单词名称:test 操作命令: (1)more list | grep -o test | wc -l (2)cat list | grep -o test | wc -l ( ...
- MyBatis-Spring 执行SQL语句的流程
1. 从SqlSessionDaoSupport开始 通常我们使用MyBatis会让自己的DAO继承SqlSessionDaoSupport,那么SqlSessionDaoSupport是如何运作的呢 ...
- 省市区 Mysql 数据库表
1.查省SELECT * FROM china WHERE china.Pid=02.查市SELECT * FROM chinaWHERE china.Pid=3300003.查区SELECT * F ...
- P134、面试题22:栈的压入、弹出序列
题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1.2.3.4.5是某栈的压栈序列,序列4,5,3,2,1是该压栈序列对 ...