我有一个场景,我需要sql server object
(或TABLE
具体),我只有table name.
我尝试使用SQL functions
,但没有帮助我。
E.g。我有一个名为USERS的表包含列userid和username。 示例数据为:USERS(1,'user 1')USERS(2,'user 2')。
另一个名为RequestsGenerated的表包含列entityTableName,columnName,entityid和requestName。示例数据为:RequestsGenerated('dbo.USERS','userid',1,'Leave Request')
我希望在结果集中看到的是具有适当用户的所有请求名称
下面复制的示例数据/查询:
CREATE TABLE #TMPUSERS(userid int, username varchar(50));
INSERT INTO #TMPUSERS VALUES (1, 'user 1')
INSERT INTO #TMPUSERS VALUES (2, 'user 2')
CREATE TABLE #TMPREQUESTS(requestid int, requesttype varchar(50), tablename varchar(50), columnname varchar(50), entityid int)
INSERT INTO #TMPREQUESTS VALUES (1, 'Leave Request', 'dbo.#TMPUSERS', 'userid', 1)
--select statement here, that return all records from #tmprequests with username
DROP TABLE #TMPREQUESTS
DROP TABLE #TMPUSERS
答案 0 :(得分:1)
您可以使用动态sql来获得此结果。
mediator.publish("LOG_IN");
或尝试这样的事情,
DECLARE @T_TABLE AS nvarchar(100) = 'TBL_ITEM_MASTER'
EXEC ('SELECT * FROM '+ @T_TABLE)
答案 1 :(得分:0)
我终于找到了解决这个问题的方法。分享,可能有助于其他人:
CREATE TABLE #TMPUSERS(userid int, username varchar(50));
INSERT INTO #TMPUSERS VALUES (1, 'user 1')
INSERT INTO #TMPUSERS VALUES (2, 'user 2')
CREATE TABLE #TMPREQUESTS(requestid int, requesttype varchar(50), tablename varchar(50), columnname varchar(50), entityid int)
INSERT INTO #TMPREQUESTS VALUES (1, 'Leave Request', 'dbo.#TMPUSERS', 'userid', 1)
--select statement here, that return all records from #tmprequests with username
DECLARE @SQL VARCHAR(MAX)
SELECT @SQL = 'select tr.requestid, tr.requesttype, tu.username from #TMPREQUESTS tr INNER JOIN ' + tablename + ' tu ON tr.entityid = tu.' + columnname from #TMPREQUESTS
EXEC(@SQL)
DROP TABLE #TMPREQUESTS
DROP TABLE #TMPUSERS