Temporary view usage

Source: Internet
Author: User
Create a temporary view based on the date Parameter
Purpose: query data of multiple tables named by Time
Alter procedure DBO. up_creattempview
(
@ Days varchar (4000), -- date (excluding time)
@ Start_time varchar (5), -- Start Time:
@ End_time varchar (5), -- End Time:
@ Query varchar (500 ),
@ Fileds varchar (500), -- field
@ Viewname varchar (50) = Null Output
)
As
Begin
Declare @ SQL varchar (4000)
Declare @ tmpview varchar (4000)
Set @ tmpview =''
--- Separator string @ days
-- Declare @ days varchar (4000)
Declare @ t varchar (50) -- data table
Declare @ tday varchar (20)
Declare @ tmpdate datetime
-- Set @ days = '2017-01-12 |'
-- Set @ tmpday =''
Declare @ I int
Set @ I = 0
While @ I <Len (@ days)
Begin
Set @ I = @ I + 1
If substring (@ days, @ I, 1) = '|'
Begin
Set @ tday = left (@ days, @ i-1) -- date that has been separated
-- Format @ tday: To 20060102
Set @ tmpdate = cast (@ tday as datetime)
----- Name of the combined table
Set @ T = datepart (yyyy, @ tmpdate)
If datepart (mm, @ tmpdate) <10
Set @ T = @ T + '0' + Cast (datepart (mm, @ tmpdate) as char (1 ))
Else
Set @ T = @ T + Cast (datepart (mm, @ tmpdate) as char (2 ))
If datepart (DD, @ tmpdate) <10
Set @ T = @ T + '0' + Cast (datepart (DD, @ tmpdate) as char (1 ))
Else
Set @ T = @ T + Cast (datepart (DD, @ tmpdate) as char (2 ))
Set @ T = 'fact _ '+ @ t
---------

Set @ days = substring (@ days, @ I + 1, Len (@ days) -- remaining date string
Set @ I = 0
-- Check whether data exists on the day of @ tmpday
-- Set @ tmpday = 'fact _ '+
-- Print @ tmpday
If exists (select * From sysobjects where id = object_id (@ T) and objectproperty (ID, 'isusertable') = 1)
Begin
-- Print @ tmpday
-- Data exists
If Len (@ tmpview)> 0
Set @ tmpview = @ tmpview + 'Union all select' + @ fileds + 'from' + @ T + 'where start_time> = ''' + @ tday + ''+ @ start_time + ''' and end_time <= ''' + @ tday + ''' + @ end_time + ''' and '+ @ Query
Else
Set @ tmpview = 'select' + @ fileds + 'from' + @ T + 'where start_time> = ''' + @ tday + ''+ @ start_time + ''' and end_time <= ''' + @ tday + ''' + @ end_time + ''' and '+ @ Query
End
-- Detect // end
End
End
-- Print @ days
-- Print @ tmpview
--- Separate string // end
If Len (@ tmpview)> 0
Begin
-- Create a zero-time attempt
-- Declare @ viewname varchar (20) -- temporary view name
Set @ viewname = 'tab' + Cast (datepart (yyyy, getdate () as varchar (4) +
Cast (datepart (mm, getdate () as varchar (2) +
Cast (datepart (DD, getdate () as varchar (2) +
Cast (datepart (HH, getdate () as varchar (2) +
Cast (datepart (SS, getdate () as varchar (2) +
Cast (datepart (MS, getdate () as varchar (4 ))
Set @ tmpview = 'create view' + @ viewname + 'as' + @ tmpview
-- Print @ tmpview
Exec (@ tmpview)
-- Temporary view created
Return 1
End
Else
Begin
Set @ viewname = ''-- no data, you cannot create a view
Return-1
End
-- Exec ('select' + @ fileds + 'from' + @ vname)
-- Exec ('drop view' + @ vname)
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.