SQL将数据插入临时表并将结果另存为视图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL将数据插入临时表并将结果另存为视图相关的知识,希望对你有一定的参考价值。
我正在尝试用以下查询做两件事:
- 编辑查询以获取仅在
'd|'
和')()'
之间的所有日期.....所以例如在clndr_data中(0||3(d|41429)()) (0||4(d|41464)( (0||0(s|09:00|f|15:00)())))
仅返回41429
。下面的查询返回'|d'
之后的所有日期。 - 将输出存储为View(Proj_ID,Clndr_ID,Dates)
DECLARE @project_name varchar(50) = 'NA63-TEN-20-05-12-01'
DECLARE @project_id int = (SELECT [proj_id] FROM [PMDB].[dbo]. [PROJECT] WHERE [proj_short_name] = @project_name)
DECLARE @calendar int = (SELECT [clndr_id] FROM [PMDB].[dbo].[PROJECT] WHERE [proj_short_name] = @project_name)
DECLARE @clndr_id int
DECLARE @walker int = 0
DECLARE @holder varchar(MAX)
DECLARE @date date
DECLARE @temp_tb table([id] int , [value] varchar(50))
SET @holder = (SELECT [clndr_data] FROM [PMDB].[dbo].[CALENDAR] WHERE [clndr_id] = @calendar )
SET @clndr_id = (SELECT [clndr_id] FROM [PMDB].[dbo].[CALENDAR] WHERE [clndr_id] = @calendar)
WHILE CHARINDEX('d|', @holder) > 0
BEGIN
SET @walker = CHARINDEX('d|', @holder) + 2
SET @date = DATEADD(D, CAST((SELECT SUBSTRING(@holder, @walker, 5)) AS int) - 2, '01/01/1900')
INSERT INTO @temp_tb VALUES (@clndr_id, @date)
SET @holder = SUBSTRING(@holder, @walker + 5, LEN(@holder))
END
SELECT * FROM @temp_tb
答案
我加入了你的PROJECT
和CALENDAR
表。然后将参数设置为proj_id
和clndr_id
的相应值,以便在INSERT
之前进行准备,因为date_value
列的值需要首先在WHILE
内处理。
连接到问题:SQL While Loop exists
DECLARE @project_name varchar(50) = 'NA63-TEN-20-05-12-01' DECLARE @project_id int DECLARE @clndr_id int DECLARE @walker int = 0 DECLARE @holder varchar(MAX) DECLARE @date date DECLARE @data varchar(MAX) DECLARE @temp_tb table([proj_id] int, [clndr_id] int , [date_value] date) SELECT @project_id = [p].[proj_id] ,@clndr_id = [p].[clndr_id] ,@holder = [c].[clndr_data] FROM [PMDB].[dbo].[PROJECT] AS [p] INNER JOIN [PMDB].[dbo].[CALENDAR] AS [c] ON [p].[clndr_id] = [c].[clndr_id] WHERE [p].[proj_short_name] = @project_name WHILE @walker <> LEN(@holder) + 1 BEGIN IF SUBSTRING(@holder, @walker, 2) = 'd|' BEGIN SET @data = SUBSTRING(@holder, @walker, 10) IF SUBSTRING(@data, LEN(@data) - 2, 3) = ')()' BEGIN SET @date = DATEADD(D, CAST(SUBSTRING(@data, 3, 5) AS int) -2, '01/01/1900') INSERT INTO @return_tb VALUES (@project_id, @clndr_id, @date) END END SET @walker = @walker + 1 END SELECT * FROM @temp_tb
如果您想将其保存为view
,请首先在function
上创建上面的代码。
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: -- Create date: -- Description: -- ============================================= CREATE FUNCTION GetProjectDates ( @project_name varchar(50) ) RETURNS @return_tb TABLE([proj_id] int, [clndr_id] int , [date_value] date) AS BEGIN -- Add the SELECT statement with parameter references here DECLARE @project_id int DECLARE @clndr_id int DECLARE @walker int = 0 DECLARE @holder varchar(MAX) DECLARE @date date DECLARE @data varchar(MAX) SELECT @project_id = [p].[proj_id] ,@clndr_id = [p].[clndr_id] ,@holder = [c].[clndr_data] FROM [PMDB].[dbo].[PROJECT] AS [p] INNER JOIN [PMDB].[dbo].[CALENDAR] AS [c] ON [p].[clndr_id] = [c].[clndr_id] WHERE [p].[proj_short_name] = @project_name WHILE @walker <> LEN(@holder) + 1 BEGIN IF SUBSTRING(@holder, @walker, 2) = 'd|' BEGIN SET @data = SUBSTRING(@holder, @walker, 10) IF SUBSTRING(@data, LEN(@data) - 2, 3) = ')()' BEGIN SET @date = DATEADD(D, CAST(SUBSTRING(@data, 3, 5) AS int) -2, '01/01/1900') INSERT INTO @return_tb VALUES (@project_id, @clndr_id, @date) END END SET @walker = @walker + 1 END RETURN END GO
然后将以下代码保存为您的视图:
SELECT * FROM [GetProjectDates]('NA63-TEN-20-05-12-01')
另一答案
我不确定,但可能在下面查询会帮助你。因为我假设日期值只有5位数。
create table #yourtable (
cola varchar(1000)
)
;
insert into #yourtable
values ('(0||3(d|41429)())')
insert into #yourtable
values ('(0||4(d|41464)( (0||0(s|09:00|f|15:00)())))')
select SUBSTRING(cola,9,5)
from #yourtable
where PATINDEX('%[0-9][0-9][0-9][0-9][0-9])()%', cola )>0
以上是关于SQL将数据插入临时表并将结果另存为视图的主要内容,如果未能解决你的问题,请参考以下文章