0

我现在正在用 SQL 编写脚本,以便可以在 Powershell 中执行输出以生成报告。代码是这样的。

declare @from datetime, @to datetime
select @from=GETDATE(), @to=GETDATE()+1

SELECT ---$shape500.text='+'"'+'Assignation de Barrières-' + DATENAME(WEEKDAY,@from)+','+DATENAME(MONTH,@from)+' '+DATENAME(DAY,@from)+','+DATENAME(YEAR,@from)+'"'
+'$shape'+cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+
'=$page.DrawRectangle'+'('+CAST(X1 as varchar)+','+CAST(Y1 as varchar)+','+CAST(X2 as varchar)+','+CAST(Y2 as varchar)+')'
+'$shape'+ cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.'+'linestyle="Gantt"'
+'$shape'+ cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.'+'textstyle="Gantt"'+case when left(arrival, 2)='AC' THEN 'Red' ELSE '' END+ CASE WHEN LEFT(Departure, 2)='AC' THEN 'Red' ELSE '' END +' '+' '
---+'$shape'+ cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.FillStyle="Gantt"'+CASE WHEN CHARINDEX()
+'$shape'+ cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.Text="'+case when LEN(arrival)<>0 THEN Arrival ELSE '###' END +'/'+ [ACType Iata]+'/'+ CASE WHEN LEN(Departure)<>0 THEN Departure ELSE '###' END +'"'
+'$shape'+ cast(100+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'=$page.DrawLine('+cast(X1 -0.5 as varchar)+','+cast(Y1 -0.03 as varchar)+','+cast(X1 +0.5 as varchar)+','+cast(Y1 -0.03 as varchar)+ ')'
+'$shape'+ cast(100+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.Text="'+CONVERT(char(5), [Allocation Start Datetime], 108)+'"'
+'$shape'+ cast(100+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.TextStyle="Times"'
+'$shape'+ cast(100+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.LineStyle="Times"'
+'$shape'+ cast(200+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'=$page.DrawLine('+cast(X2 -0.1 as varchar)+','+cast(Y1 -0.03 as varchar)+','+cast(X1 +0.1 as varchar)+','+cast(Y1 -0.03 as varchar)+ ')'
+'$shape'+ cast(200+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.Text="'+CONVERT(char(5), [Allocation End Datetime], 108)+'"'
+'$shape'+ cast(200+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.TextStyle="Times"'
+'$shape'+ cast(200+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.LineStyle="Times"'
+'$shape'+ cast(300+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'=$page.DrawLine('+cast(X1 as varchar)+','+cast(Y2 as varchar)+','

FROM GanttReports(default,default)

GanttReport是一个存储过程。输出将在一个大字符串中生成所有内容,例如

$shape1=$page.DrawRectangle(42.666660,6.78,58.083330,7.025)$shape1.linestyle="甘特图"$shape1.textstyle="甘特图" $shape1.Text="QK8718/DH1/QK8749"$shape101=$page. DrawLine(42.166660,6.75,43.166660,6.75)$shape101.Text="16:21"$shape101.TextStyle="Times"$shape101.LineStyle="Times"$shape201=$page.DrawLine(57.983330,6.75,42.766660, 6.75)$shape201.Text="17:30"$shape201.TextStyle="Times"$shape201.LineStyle="Times"$shape301=$page.DrawLine(42.666660,7.025)

但是我想要的是在单独的行中。你可以说使用'$shape'作为分隔符来分割它吗?

我知道我可以在论坛中搜索时创建拆分功能,但是不知道在这种情况下如何使用它。我是 SQL Server 的新手,如果有人可以提供帮助,我将不胜感激!

4

1 回答 1

0

您需要将 GanttReports 的结果存储在 varchar 类型的变量中

喜欢

Decalre @str varchar(max)

选择 @str = '$shape'+cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar) FROM GanttReports(default,default)

然后使用 split 函数返回一个表集。为了使用下面的拆分函数,我使用了 dbo.seq 表,该表填充了从 1 到 1000 的数字,它用于以行的形式返回你的结果。

IF OBJECT_ID('dbo.GetNums') 不是 NULL 删除函数 dbo.GetNums; GO CREATE FUNCTION dbo.GetNums(@n AS BIGINT) 返回表作为返回 L0 AS(SELECT 1 AS c UNION ALL SELECT 1), L1 AS(SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B), L2 AS (从 L1 中选择 1 AS c 作为交叉连接 L1 作为 B),L3 AS(从 L2 中选择 1 作为 c 作为交叉连接 L2 作为 B),L4 AS(从 L3 中选择 1 作为 c 作为交叉连接 L3 作为 B ), L5 AS(SELECT 1 AS c FROM L4 AS A CROSS JOIN L4 AS B), Nums AS(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS n FROM L5) SELECT n FROM Nums WHERE n <= @ n;

创建表 dbo.seq (nbr bigint) 去

插入 seq Select * from dbo.GetNums(1001)

创建函数 [dbo].[fn_Delimitter] (@string nvarchar(MAX), @delimiter varchar(100)) 返回表作为返回 ( select str = rtrim(ltrim(substring(@delimiter + @string + @delimiter, nbr + len (@delimiter), charindex(@delimiter, @delimiter + @string + @delimiter, nbr + len(@delimiter)) - nbr - len(@delimiter)))) 来自 seq 其中 nbr <= len(@delimiter + @字符串 + @delimiter) - len(@delimiter) 和子字符串 (@delimiter + @string + @delimiter, nbr, len(@delimiter)) = @delimiter)

声明@str varchar(max)

选择@str = '$shape1=$page.DrawRectangle(42.666660,6.78,58.083330,7.025)$shape1.linestyle="Gantt"$shape1.textstyle="Gantt" $shape1.Text="QK8718/DH1/QK8749"$ shape101=$page.DrawLine(42.166660,6.75,43.166660,6.75)$shape101.Text="16:21"$shape101.TextStyle="Times"$shape101.LineStyle="Times"$shape201=$page.DrawLine(57.983330 ,6.75,42.766660,6.75)$shape201.Text="17:30"$shape201.TextStyle="Times"$shape201.LineStyle="Times"$shape301=$page.DrawLine(42.666660,7.025)'

从 dbo.fn_Delimitter 中选择 *

于 2012-12-30T06:33:53.547 回答