查询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 ...
随机推荐
- python多进程中的队列数据共享问题
0x00 起 今天在写一个小东西的时候,需要控制并发量,但又不能直接调用python multiprocessing(问题会在文后提到).于是尝试用Queue来实现. 最一开始的思路是这样的: fro ...
- 轻仿QQ音乐之音频歌词播放、锁屏歌词-b
先上效果图 歌词播放界面 音乐播放界面 锁屏歌词界面 一. 项目概述 前面内容实在是太基础..只想看知识点的同学可以直接跳到第三部分的干货 项目播放的mp3文件及lrc文件均来自QQ音乐 本文主要主要 ...
- 为你的PHP程序选择合适的密码库(初稿)
如果本文中的术语让你感到疑惑,请先参阅密码学术语及概念一文. 密码学不是魔术.加密一个应用程序并不能保证它在袭击下的安全(特别是在你没有设置验证密文的情况下).但如果出于商业需求你要确保程序的安全,传 ...
- python参考手册--第4、5、6、7章
1.zip zip(s,t):将序列组合为一个元组序列[(s[0],t[0]), (s[1],t[1]), (s[2],t[2]), (s[3],t[3]),...] >>> s = ...
- 正确使用STL-MAP中Erase函数
一切尽在代码中. #include <iostream> #include <map> #include <string> using namespace std ...
- java thread类和runable
java thread 类其实和其他类是一样的.有自己的属性和方法.以及有一个重写的run方法 不同的是:必须重写run()方法. run()方法是线程thread启动后cpu调用运行的程序代码. r ...
- [主席树]HDOJ4417 Super Mario
题意:n个数 m个询问 ($n.m \le 10^5$) 每个询问有l, r, k 问的是[l, r]区间内有多少个数小于等于k 用主席树做的话查询第i小的数与k比较即可 #define lson ...
- Servlet课程0424(一) 通过实现Servlet接口来开发Servlet
//这是我的第一个Servlet,使用实现Servlet接口的方式来开发 package com.tsinghua; import javax.servlet.*; import java.io.*; ...
- DAO是什么技术
DAO是Data Access Object数据访问接口,数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据库资源中间. 在核心J2EE模式中是这样介绍DAO模式的:为了建立一个健壮的J2EE应 ...
- 控件如何在IDE中响应MouseDown事件
自己做了一个pagecontrol控件,点击page页可以切换,运行时没有问题,但在设计时控件放到窗体后好像不响应mouse事件,导致设计期不能切换page页,有知道的朋友还请不吝赐教,谢谢. CM_ ...