我想将获得的查询(公开,见:代码)转换为新表,以便我可以更轻松地计算和处理数据。
我知道这个问题听起来很愚蠢,但是当我使用时:
SELECT *
INTO Price_to_book_valid
FROM
(
select *
from pricetobook
declare @sql nvarchar(max);
declare @fields nvarchar(max);
set @fields=stuff((select ',['+column0+']'
from dll_ptbv
where column0 not in ('Code','Mnemonic')
group by column0
order by case when column0='Name' then 0 else 3 end
,column0
for xml path('')
,type
).value('.','nvarchar(max)')
,1 ,1 ,'');
set@sql='select'+@fields
+ 'from (select column0
,column1
,sum(rn1*rn2) over(order by rn2) as rn
from (select column0
,column1
,case when column0=''Name'' then 1 else 0 end as rn1
,row_number() over(order by (select null)) as rn2
from dll_ptbv
) as a
) as a
pivot (max(column1) for column0 in ('+@fields+')) as p
order by [Name]';
execute sp_executesql @sql;
它向我发送了一条错误消息,我应该如何在上面提到的代码中输入我的查询..
错误:
Msg 156,Level 15,State 1,Line 10
关键字附近的语法不正确'选择'。
Msg 156,Level 15,State 1,Line 54
关键字'来自'
附近的语法不正确代码:
Skiping the first 7 characters ( "add to " ) from the beginning using the regular expression (?:^.{7}) and then splitting it with either with comma or semicolon [,;]
String entry1 = "add to xx16,John Doe";
String entry2 = "add to ab20,John Doe;Richard Roe;John Stiles";
String[] str = entry1.split("(?:^.{7})|[,;]");
for(String st : str ){
System.out.println(st);
}
str = entry2.split("(?:^.{7})|[,;]");
for(String st : str ){
System.out.println(st);
}
答案 0 :(得分:0)
您可以在列名称
之前使用选择INTOSELECT * INTO NewTable
FROM
(
SELECT .... complex query ....
FROM ...
...
) p <<<<<<<< remove as
(你应该像a中一样删除)拳头你应该在使用alais之后指定一个表名并且最终
答案 1 :(得分:0)
- 14.从动态存储过程执行中选择INTO新表。
USE tempdb;
GO
-- SQL create dynamic stored procedure
CREATE PROCEDURE DynamicSprocToTempTable
AS
BEGIN
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'SELECT top (5) * from AdventureWorks.Purchasing.PurchaseOrderHeader
ORDER BY NEWID()'
-- Dynamic SQL
EXEC Sp_executesql @SQL
END
GO
-- Test stored procedure
EXEC tempdb.dbo.DynamicSprocToTempTable
GO
-- Select into temporary table from OPENQUERY
-- SQL select into temp table
SELECT * INTO #poh
FROM Openquery(SERVERALPHA,'exec tempdb.dbo.DynamicSprocToTempTable')
GO
SELECT PurchaseOrderID, VendorID, OrderDate
FROM #poh
/ *结果
PurchaseOrderID VendorID OrderDate
2662 50 2004-05-19 00:00:00.000
2454 44 2004-05-01 00:00:00.000
2547 96 2004-05-10 00:00:00.000
901 54 2003-10-13 00:00:00.000
2675 74 2004-05-22 00:00:00.000
* /
GO