首页 \ 问答 \ 通过动态传递表名和列名来反转COLUMN DATA(Reversing COLUMN DATA by dynamically passing table name and Column Name)

通过动态传递表名和列名来反转COLUMN DATA(Reversing COLUMN DATA by dynamically passing table name and Column Name)

我想通过传递表名和列名来动态地反转COLUMN DATA

DECLARE @TABLE_NAME  NVARCHAR(300)   -- to pass table name dynamically
DECLARE @COLUMN_NAME NVARCHAR(300)   -- to pass column name dynamically

SET @TABLE_NAME = 'TEST1'   -- Passing table name
SET @COLUMN_NAME = 'SSN'    -- Passing column Name

DECLARE @OUTPUT NVARCHAR(MAX)   -- to pass the selected column data to update

SET @OUTPUT = 'SELECT'+' '+@COLUMN_NAME+' '+'FROM'+@TABLE_NAME

DECLARE @UPDATE_EXEC NVARCHAR(MAX)    -- To exec the Final update result 
SET @UPDATE_EXEC = 'UPDATE ['+@TABLE_Name + ']'+'SET'+' '+@COLUMN_NAME+
'=REVERSE(EXEC (@output))'

EXEC(@UPDATE_EXEC)

但是我收到这些错误:

Msg 156,Level 15,State 1,Line 1
关键字'EXEC'附近的语法不正确。

Msg 137,Level 15,State 2,Line 1
必须声明标量变量“@output”


I want to reverse a COLUMN DATA dynamically by passing table name and Column Name

DECLARE @TABLE_NAME  NVARCHAR(300)   -- to pass table name dynamically
DECLARE @COLUMN_NAME NVARCHAR(300)   -- to pass column name dynamically

SET @TABLE_NAME = 'TEST1'   -- Passing table name
SET @COLUMN_NAME = 'SSN'    -- Passing column Name

DECLARE @OUTPUT NVARCHAR(MAX)   -- to pass the selected column data to update

SET @OUTPUT = 'SELECT'+' '+@COLUMN_NAME+' '+'FROM'+@TABLE_NAME

DECLARE @UPDATE_EXEC NVARCHAR(MAX)    -- To exec the Final update result 
SET @UPDATE_EXEC = 'UPDATE ['+@TABLE_Name + ']'+'SET'+' '+@COLUMN_NAME+
'=REVERSE(EXEC (@output))'

EXEC(@UPDATE_EXEC)

But I'm getting these errors:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'EXEC'.

Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@output"


原文:https://stackoverflow.com/questions/41886782
更新时间:2019-10-16 04:38

最满意答案

您正试图将EXEC放入更新查询中,但不允许。 EXEC用于执行查询,它不能成为任何查询的一部分。

要颠倒表格中的任何一列,您可以在下面这样做,

SET @UPDATE_EXEC ='UPDATE ['+ @ TABLE_Name +']'+'SET'+''+ @ COLUMN_NAME +'= REVERSE('+ @ COLUMN_NAME +')';

EXEC(@UPDATE_EXEC);


You are trying to put EXEC in your update query, its not allowed. EXEC is use to execute the query, it can't be part of any query.

To reverse any column in your table you can do so like below,

SET @UPDATE_EXEC = 'UPDATE ['+@TABLE_Name + ']'+'SET'+' '+@COLUMN_NAME+ '=REVERSE('+@COLUMN_NAME+')';

EXEC(@UPDATE_EXEC);

2017-01-27

相关问答

更多

去 - 解析不是“标准”格式的日期/时间字符串(Go - Parsing date/time strings which are not 'standard' formats)

有一些关键的值,时间.Parse正在寻找。 通过改变: test, err := time.Parse("10/15/1983", "10/15/1983") 至 test, err := time.Parse("01/02/2006", "10/15/1983") 解析器将会识别它。 这是操场上修改的代码 。 package main import "fmt" import "time" func main() { test, err := time.Parse("01/02/

Firebase允许拒绝(Permission denied with Firebase)

您可以提供".read": "auth != null"以允许所有授权用户读取users对象下的数据并防止未经授权的用户阅读它。 You could give ".read": "auth != null" to allow all authorised users to read data under users object and prevent unauthorised users from reading it.

Scala / Clojure使用什么汇编程序?(What assembler did Scala/Clojure use? [closed])

Clojure使用ASM库直接生成字节码。 ASM是一个非常好的和广泛使用的库。 Clojure uses the ASM library to generate bytecode directly. ASM is a very good and widely used library for this purpose.

Python中的模块化编译器(Modular Compiler in Python)

您想使用像stevedore这样的现有插件库。 它将为您提供枚举可导入文件的工具,以及导入这些模块的工具。 You want to use an existing plugin library like stevedore. It will give you the tools to enumerate files that can be imported, and tools to import those modules.

PrimeFaces条码样本(PrimeFaces Barcode sample)

您是否已将barcode4j和qrgen库添加到依赖项中? <dependency> <groupId>net.glxn</groupId> <artifactId>qrgen</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>net.sf.barcode4j</groupId> <artifactId>barcode4j-light</artifactId>

UTF16十六进制到文本(UTF16 hex to text)

没错,Postgres不支持UTF-16。 但是,它确实支持Unicode转义序列 : SELECT U&'\0633\0644\0627\0645' 但请记住, Unicode代码点和UTF-16代码单元仅在Basic Multilingual Plane中是等效的。 换句话说,如果您有任何跨越多个16位代码单元的UTF-16字符,则需要自己将它们转换为相应的代码点。 That's right, Postgres doesn't support UTF-16. However, it doe

Arch Linux中没有OpenGL 3标头(No OpenGL 3 headers in Arch Linux)

你不需要GL3.h,GL3.h只是摆脱了所有弃用的功能。 所以只需要包含gl.h而不是gl3.h 下载和手动copyieng到/usr/include/GL/不是一个真正的解决方案,它的工作原理是肯定的,但如果添加到mesa包中的gl3.h怎么gl3.h ? 你是包经理会发现冲突。 最好将gl3.h添加到项目或主目录中,并将其添加到$ PATh或使用-I标志。 You don't need a GL3.h, the GL3.h just got rid of all deprecated fea

在Oracle中使约束可以推迟(Make Constraint Deferrable in Oracle)

它必须被删除并重新创建。 It has to be dropped and recreated.

相关文章

更多

最新问答

更多
  • 在svg - gstatic urls中嵌入'Open Sans'字体(Embedding 'Open Sans' font in svg - gstatic urls)
  • 停止python线程等待popen?(stopping a python thread waiting for popen?)
  • Julia:如何从文本文件导入图形(csv具有不等数量的'列')?(Julia: How to import a graph from text file (csv with unequal number of 'columns')?)
  • 在另一个插件中使用qgis核心插件(Useage of qgis core plugin in another plugin)
  • jQuery .get缓存工作得太好了?(jQuery .get caching working too well?)
  • Gameplay3D gameplay :: TimeListener(Gameplay3D gameplay::TimeListener)
  • 获取ItemDataBound中的行数(Get row count in ItemDataBound)
  • NHibernate SchemaExport(config).Create(false,true)不会使用SQL Server 2014 LocalDB创建数据库架构(NHibernate SchemaExport(config).Create(false, true) does not create DB Schema with SQL Server 2014 LocalDB)
  • 列出迭代器失效(List iterator disfunctionality)
  • 列出多个变量的理解(List comprehension with multiple variables)
  • 如何在asp.net MVC5中显示锁定结束时间(How to Display locking end time in asp.net MVC5)
  • 在android中的ImageView上显示TextView(Display TextView over ImageView in android)
  • 64位sharemem.pas / borlndmm.dll XE2(64-bit sharemem.pas/borlndmm.dll XE2)
  • 为什么分离和追加比直接更改html更有效?(Why is detaching and appending more efficient than changing html directly?)
  • Eclipse将“排序成员”设置保存到repo(Eclipse Saving “Sort members” settings to repo)
  • 当我查看最近标记的instagram API时,为什么帖子似乎会改变(why do the posts seem to change when i look at the recent tagged instagram API)
  • 使用php邮件功能发送电子邮件 - 变慢[关闭](Sending email using php mail function - Getting slow [closed])
  • Web服务器可以写入登录用户的屏幕吗?(Can a web server write to a logged-in user's screen?)
  • CFNetwork匿名密码?(CFNetwork Anonymous Ciphers?)
  • 更快的算法来计算一个范围内的特定整数可以分割多少个数字(Faster algorithm to count how many numbers are divisible by a specific integer in a range)
  • 用于散点图的Matplotlib FuncAnimation(Matplotlib FuncAnimation for scatter plot)
  • Gradle:Manifest merge更改AndroidManifest文件中“theme”参数的值:(Gradle: Manifest merge changes the value for the “theme” parameters in the AndroidManifest file:)
  • 如何在属性中渲染cms / static块(How do I render cms/static blocks in an attribute)
  • Javascript显示错误和页面重新加载问题(Javascript display error and page reload problems)
  • android官方固件签名(android official firmware signature)
  • 在这种情况下,有比eval()更好的方法吗?(Is there a better way than eval() in this scenario?)
  • primefaces commandbutton actionlistener未调用(primefaces commandbutton actionlistener not called)
  • C将指针传递给堆栈上的数组(C passing a pointer to an array on the stack)
  • 简单的产品内部可配置产品(Simple product inside configurable product)
  • nhibernate HbmMapping到Xml(nhibernate HbmMapping to Xml)