查询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 ...
随机推荐
- dnf脚本的研究
[player number] 2 8 [pvp start area]0 0 0 00 0 0 00 0 0 0[dungeon]62[/dungeon][type] `[normal]`[gre ...
- Unity3D中Ragdoll的用法
一.创建Ragdoll 见unity3d组件文档里的Ragdoll Wizard.由于unity3d中的Ragdoll设置的骨骼点名字与3DMAX里人体骨骼命名有些不一样,下图为Unity3 ...
- MySQL中字符串函数详细介绍
MySQL字符串函数对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str)返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回N ...
- php如何查找会员无限分类的所有上级和所有下级
a推广出的a-1,a-2继续推广,得到a-1-1,a-1-2等等数据库设计思路如下:用户表中有一个son这么一个字段,这个字段中存放名下所有会员的id,用分号隔开.这个字段的维护:比如a-1-1推广出 ...
- Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed MySQLNonT ...
- COUNT(*)与COUNT(列名)的区别(转)
COUNT(*)与COUNT(列名)的区别 以前一直没有留意到COUNT(*)与COUNT(列名)的区别,昨天晚上无意中看到数据库系统工程师教程里面的一句话."如果null参与聚 ...
- Titan DB的一些问题
使用熟悉一点的系统来测试TitanDB,HBASE+ES,记录下来一些小tips. 1.首先TitanDB支持的Hadoop只有1.2.1,所以Hbase自然也只能取到0.98,虽然官网上提供了tit ...
- snoopy(强大的PHP采集类) 详细介绍
Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单,可以用来开发一些采集程序和小偷程序,本文章详细介绍snoopy的使用教程. Snoopy的一些特点: 抓取网页的内容 fe ...
- linux命令补全 忘记命令只记得开头
linux的shell不仅提供上下箭头来翻阅历史使用过的命令,还提供命令补全功能. 例如,你想创建一个文件夹,只记得是m开头的命令,此时可以: ①输入m ②按键盘上的Tab键两次 (有可能还出现这句话 ...
- appserv在哪修改服务器名
修改服务器名,需要修改httpd.conf文件中的第233行 ServerName 192.168.1.104:8080. 这个文件位于 AppServ\Apache2.2\conf\ 下.