我错过了什么?我正在尝试使用新列更改表并插入其中。最后,我想插入一个变量@loopcounter
。但它似乎没有认识到新专栏。
这是表格
SELECT
[dbo].[webpages_UsersInRoles].[UserId],
[dbo].[UserProfile].[UserName],
[dbo].[UserProfile].[CustomerId],
[dbo].[webpages_UsersInRoles].[RoleId],
[dbo].[webpages_Roles].[RoleName]
INTO
#ControlTable
FROM
[dbo].[webpages_UsersInRoles]
INNER JOIN
[dbo].[UserProfile] ON [dbo].[webpages_UsersInRoles].[UserId] = [dbo].[UserProfile].UserId
INNER JOIN
[dbo].[webpages_Roles] ON [webpages_UsersInRoles].[RoleId]=[dbo].[webpages_Roles].[RoleId]
WHERE
CustomerId = 'DEMO'
改变/插入:
ALTER TABLE #ControlTable
ADD Loopfield INT;
INSERT INTO #ControlTable (Loopfield)
VALUES (1)
SELECT * FROM #ControlTable;
答案 0 :(得分:0)
我想你想要:
alter table #ControlTable add LoopField int identity(1, 1);
或者只是使用标识列创建表:
SELECT identity(1, 1) as loopfield,
[dbo].[webpages_UsersInRoles].[UserId],
[dbo].[UserProfile].[UserName],
[dbo].[UserProfile].[CustomerId],
[dbo].[webpages_UsersInRoles].[RoleId],
[dbo].[webpages_Roles].[RoleName]
或者,如果您不打算在表格中插入新行,row_number()
就足够了:
SELECT row_number() over (order by (select null)) as loopfield,
[dbo].[webpages_UsersInRoles].[UserId],
[dbo].[UserProfile].[UserName],
[dbo].[UserProfile].[CustomerId],
[dbo].[webpages_UsersInRoles].[RoleId],
[dbo].[webpages_Roles].[RoleName]
答案 1 :(得分:0)
对我来说效果很好。
create table #temp (name varchar(10));
insert into #temp values ('bob'), ('mary');
ALTER TABLE #temp
ADD Loopfield INT;
declare @Loopfield int = 1;
INSERT INTO #temp (Loopfield)
VALUES (@Loopfield);
select * from #temp;
drop table #temp;