如何从其他表中获取列的名称 [英] How can i get the name of the column from other table
问题描述
我希望在'AS'之后从查询中获取我的列名,这就是我正在做什么并且它不起作用,我试过动态sql不能和我一起工作
例如:选择经理AS'从manager_table中选择mytable'中的column_Name
所以我想要不同名称的经理列名我从mytable中选择
有什么建议吗?
谢谢
我尝试了什么:
所以例如:select manager AS '从manager_table中选择column_Name来自manager_table
i want to get my column name from query after 'AS' , thats what im doing and its not working , i tried dynamic sql not working with me
so for example : select manager AS 'select column_Name from mytable' from manager_table
so i want the manager column name with different name i select it from mytable
any suggestions?
Thank you
What I have tried:
so for example : select manager AS 'select column_Name from mytable' from manager_table
推荐答案
当然。这是一个例子...
Sure. Here's an example ...
USE [cpqaAnswers]
GO
CREATE SCHEMA [cpqa]
GO
CREATE TABLE [cpqa].[tbl_AP_zeropadSolution003]
(
P_Id int,
amount varchar(50)
)
INSERT INTO [cpqa].[tbl_AP_zeropadSolution003]
VALUES(1, '2340'),
(2, '4568'),
(3, '10000')
SELECT * FROM [cpqa].[tbl_AP_zeropadSolution003]
SELECT * FROM [cpqa].[tbl_AP_zeropadSolution003] ORDER BY [amount]
/* how many places is the question ... arbitrarily pick a number, here six (6) ... but it doesn't really matter */
SELECT [P_Id], LEFT(REPLICATE('0', 6), 6 - LEN([amount])) + [amount] AS [zeropadded_amount] FROM [cpqa].[tbl_AP_zeropadSolution003]
SELECT [P_Id], LEFT(REPLICATE('0', 6), 6 - LEN([amount])) + [amount] AS [zeropadded_amount] FROM [cpqa].[tbl_AP_zeropadSolution003] ORDER BY [zeropadded_amount] DESC
这个例子可以追溯到05/23/13它是一个零填充例程
结果:
This example goes back to 05/23/13 and it is a zero-padding routine
The result:
/*
P_Id zeropadded_amount
1 002340
2 004568
3 010000
*/
这篇关于如何从其他表中获取列的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!