我在SQL Server中有一个变量:
DECLARE @cols AS NVARCHAR(MAX);
变量@cols
包含以下值:
[Afghanistan],[Australia],[Bangladesh]
在存储过程的某个地方,我有这个SQL语句:
SELECT ID, AccountTypeName AS Type
FROM cte AS t
我希望使用@cols
中存储的列名执行此SQL语句 -
像这样
SELECT ID, AccountTypeName, [Afghanistan], [Australia], [Bangladesh],[England] AS Type
FROM cte AS t
我的问题是我正在使用CTE。
答案 0 :(得分:0)
您可以使用动态T-SQL语句:
from tkinter import *
root = Tk()
def frame():
Frame(root, bg="red", width=100, height=100).pack(padx=5, pady=5)
def window():
Toplevel(root)
framebutton = Button(root, text="Frame", command=frame)
framewindow = Button(root, text="Window", command=window)
framebutton.pack()
framewindow.pack()
root.mainloop()
答案 1 :(得分:0)
但是使用nodetool refresh
命令检查查看的内容
完整动态Print
查询方法就在这里
T-SQL
你怎么能绑定不属于DECLARE @cols NVARCHAR(MAX), @Query NVARCHAR(MAX);
SET @cols = '[Afghanistan],[Australia],[Bangladesh]';
SET @Query = N';WITH CTE AS (SELECT NULL [DATA]) SELECT [DATA], '+@cols+' FROM CTE';
PRINT @Query;
EXEC sp_executesql @Query;
的列? &安培;为什么?
其他方式
但是,您可以直接在CTE
中绑定动态列,如下所示:
CTE
@Query结果:
SET @cols = 'NULL [Afghanistan],NULL [Australia],NULL [Bangladesh]';
SET @Query = N';WITH CTE AS (SELECT NULL [DATA], '+@cols+') SELECT * FROM CTE';
EXEC sp_executesql @Query结果:
;WITH CTE AS (SELECT NULL [DATA], NULL [Afghanistan],NULL [Australia],NULL [Bangladesh]) SELECT * FROM CTE