0

不确定如何创建此 UDF。我已经阅读了现有的问题,但不确定语法。

我想将此查询另存为视图,但不能因为我已经有声明

DECLARE @STARTDATE DATETIME
DECLARE @ENDDATE DATETIME

SELECT @ENDDATE = CAST(CONVERT(CHAR(10), GETDATE(), 101)+' 00:00:00' AS DATETIME)
SELECT @STARTDATE = DATEADD(D, -1, @ENDDATE)--'-7'
SELECT @ENDDATE = CAST(CONVERT(CHAR(10), GETDATE(), 101)+' 23:59:59' AS DATETIME)

SELECT
    LTRIM(CIJ.ORDERACCOUNT) 'ACCOUNT'
    ,LTRIM(CIJ.SALESID) 'TOMKIN ORDER #'
    ,LTRIM(CIJ.INVOICEID) 'TOMKIN INVOICE #'
    ,CIJ.PURCHASEORDER 'YOUR ORDER #'
FROM 
    CUSTINVOICEJOUR CIJ
WHERE 
    CIJ.INVOICEDATE >= @STARTDATE AND CIJ.INVOICEDATE <= @ENDDATE
ORDER BY 
    CIJ.INVOICEDATE,CIJ.INVOICEID

任何帮助将不胜感激

4

1 回答 1

0

这是您的查询转换为视图。请注意,您不能将参数传递到视图中,也不能指定顺序。

CREATE VIEW Makdaddy ([ACCOUNT], [TOMKIN ORDER #], [TOMKIN INVOICE #], [YOUR ORDER #])
AS
SELECT
     LTRIM(CIJ.ORDERACCOUNT) 'ACCOUNT'
   , LTRIM(CIJ.SALESID) 'TOMKIN ORDER #'
   , LTRIM(CIJ.INVOICEID) 'TOMKIN INVOICE #'
   , CIJ.PURCHASEORDER 'YOUR ORDER #'
FROM CUSTINVOICEJOUR CIJ
WHERE CIJ.INVOICEDATE >= DATEADD(day,-14,GETDATE())
  AND CIJ.INVOICEDATE <= GETDATE()

当您调用视图时,您可以对列进行排序并执行额外的过滤。

SELECT *
FROM Makdaddt
ORDER BY [ACCOUNT]

这是您的查询转换为 udf。这可能是更好的方法,因为您的开始日期可能会改变。

CREATE FUNCTION dbo.Makdaddy (@StartDate DATETIME)
RETURNS TABLE
AS
RETURN 
(
  SELECT
       LTRIM(CIJ.ORDERACCOUNT) 'ACCOUNT'
     , LTRIM(CIJ.SALESID) 'TOMKIN ORDER #'
     ,  LTRIM(CIJ.INVOICEID) 'TOMKIN INVOICE #'
     , CIJ.PURCHASEORDER 'YOUR ORDER #'
  FROM CUSTINVOICEJOUR CIJ
  WHERE CIJ.INVOICEDATE >= @StartDate
    AND CIJ.INVOICEDATE <= GETDATE()
);
于 2013-02-20T02:01:43.557 回答