SQL Server to stitch the arguments to the string, feedback the result set

Source: Internet
Author: User

ALTER PROCEDURE [dbo]. [Usp_visit_detail]
(@siteid BIGINT,
@Startime VARCHAR (),
@Endtime VARCHAR (),
@Aflag INT = 10,--initial value of 10, not 10, indicates an incoming parameter
@Area VARCHAR = ' n ',----an initial value of n, not n, indicates an incoming parameter
@Nflag INT = 10,-The initial value is 10, not 10, it indicates an incoming parameter
@VALUE1 varchar = ' n '--initial value n, not n, indicates an incoming parameter


)
as
BEGIN
DECLARE @AFlag0 varchar (),
@NFlag0 varchar (),
[email protected] varchar,
@Sqlstr varchar (+)
          
IF @AFlag = ten
SET @AFlag0 = ' '
ELSE
SET @AFlag0 = ' and aflag= ' + CAST (@AFlag as VARCHAR)


IF @Area = ' N '
SET @Area = ' '
ELSE
SET @Area = ' and province= ' + ' + ' + @Area + '
        
IF @NFlag = ten
SET @NFlag0 = ' '
ELSE
SET @NFlag0 = ' and nflag= ' + CAST (@NFlag as VARCHAR)

IF @Value1 = ' N '
SET @Value1 = ' '
ELSE
SET @Value1 = ' and value1= ' + ' + ' + @Value1 + '
   
SET @Sqlstr = ' SELECT a.intime, C.visitornm, C.vcount, (case is a.value2 = ' + ' + ' + ' + ' + ' + ' then a.value1 + ' + ') ' + ' + ' when a.value2 = ' + ' + ' + ' + ' then a.value1 + ' + ' + ' + ' + ' + ' + ' + a.value2 END ' as Source0,
DATEDIFF (Ms,a.intime, A.outtime) as Vtime, A.pages, A.REV1, dbo. Get_url (A.siteid, A.inurlid) as Inrul, dbo. Get_url (A.siteid, a.outurlid) as Outrul
From dbo. f_pages_t as a INNER JOIN (SELECT VisitorID, MAX (ID) as ID from dbo. f_pages_t WHERE SiteID = ' + ' +convert (VARCHAR, @siteid) + ' + ' and day0>= ' [email protected]+ ' and day0<= ' [email Protected][email protected]
[email protected]+ @Nflag0 + @Value1 + ' GROUP by VisitorID) As B on a.id = b.ID left OUTER JOIN dbo. F_visit as C on a.visitorid = C.visitorid '
                      
PRINT @sqlstr
EXEC (@sqlstr)
END

Related Article

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.