一尘不染

如何在SQL Server 2008表中创建计算列

sql

我真的需要一个具有简单总和的表上的计算列。

请看下面:

SELECT   key3
         ,SUM(UTOTALWBUD)
FROM     CONTACT1
         INNER JOIN CONTACT2
            ON CONTACT1.ACCOUNTNO = CONTACT2.ACCOUNTNO
WHERE    KEY1 = 'Client'
GROUP BY KEY3

我试图通过添加以下内容来创建计算列

ALTER TABLE ManagerTaLog
ADD         WeeklyBudget as (   SELECT
                                        key3
                                        ,SUM(UTOTALWBUD)
                            FROM        CONTACT1
                                        JOIN CONTACT2
                                            ON CONTACT1.ACCOUNTNO = CONTACT2.ACCOUNTNO
                            WHERE       KEY1 = 'Client'
                            GROUP BY    KEY3)

我收到错误消息:

在这种情况下,不允许消息1046,级别15,状态1,第4行子查询。仅允许标量表达式。

请告知我该怎么办。

非常感谢

第2部分

我已经创建了一个函数;但是,我得到空值,请指教。

CREATE FUNCTION [dbo].[SumIt](@Key3 varchar)
RETURNS TABLE 
AS
RETURN
(
SELECT      SUM(UTOTALWBUD)
FROM        CONTACT1
            JOIN CONTACT2
                ON CONTACT1.ACCOUNTNO = CONTACT2.ACCOUNTNO
            JOIN Phone_List
                ON CONTACT1.KEY3 = Phone_List.[Manager ]
WHERE       KEY1 = 'Client'
            AND Phone_List.[Manager ] = @Key3
GROUP BY [Manager ]

)
END

GO

只需选择返回要添加到Phone_list表中的值的语句

SELECT      [Manager ]
            ,SUM(UTOTALWBUD)
FROM        CONTACT1
            JOIN CONTACT2
                ON CONTACT1.ACCOUNTNO = CONTACT2.ACCOUNTNO
            JOIN Phone_List
                ON CONTACT1.KEY3 = Phone_List.[Manager ]
WHERE       KEY1 = 'Client'
GROUP BY    [Manager ]

表定义

CREATE TABLE [dbo].[CONTACT1](
    [ACCOUNTNO] [varchar](20) NOT NULL,
    [COMPANY] [varchar](40) NULL,
    [CONTACT] [varchar](40) NULL,
    [LASTNAME] [varchar](15) NULL,
    [DEPARTMENT] [varchar](35) NULL,
    [TITLE] [varchar](35) NULL,
    [SECR] [varchar](20) NULL,
    [PHONE1] [varchar](25) NOT NULL,
    [PHONE2] [varchar](25) NULL,
    [PHONE3] [varchar](25) NULL,
    [FAX] [varchar](25) NULL,
    [EXT1] [varchar](6) NULL,
    [EXT2] [varchar](6) NULL,
    [EXT3] [varchar](6) NULL,
    [EXT4] [varchar](6) NULL,
    [ADDRESS1] [varchar](40) NULL,
    [ADDRESS2] [varchar](40) NULL,
    [ADDRESS3] [varchar](40) NULL,
    [CITY] [varchar](30) NULL,
    [STATE] [varchar](20) NULL,
    [ZIP] [varchar](10) NOT NULL,
    [COUNTRY] [varchar](20) NULL,
    [DEAR] [varchar](20) NULL,
    [SOURCE] [varchar](20) NULL,
    [KEY1] [varchar](20) NULL,
    [KEY2] [varchar](20) NULL,
    [KEY3] [varchar](20) NULL,
    [KEY4] [varchar](20) NULL,
    [KEY5] [varchar](20) NULL,
    [STATUS] [varchar](3) NOT NULL,
    [NOTES] [text] NULL,
    [MERGECODES] [varchar](20) NULL,
    [CREATEBY] [varchar](8) NULL,
    [CREATEON] [datetime] NULL,
    [CREATEAT] [varchar](5) NULL,
    [OWNER] [varchar](8) NOT NULL,
    [LASTUSER] [varchar](8) NULL,
    [LASTDATE] [datetime] NULL,
    [LASTTIME] [varchar](5) NULL,
    [U_COMPANY] [varchar](40) NOT NULL,
    [U_CONTACT] [varchar](40) NOT NULL,
    [U_LASTNAME] [varchar](15) NOT NULL,
    [U_CITY] [varchar](30) NOT NULL,
    [U_STATE] [varchar](20) NOT NULL,
    [U_COUNTRY] [varchar](20) NOT NULL,
    [U_KEY1] [varchar](20) NOT NULL,
    [U_KEY2] [varchar](20) NOT NULL,
    [U_KEY3] [varchar](20) NOT NULL,
    [U_KEY4] [varchar](20) NOT NULL,
    [U_KEY5] [varchar](20) NOT NULL,
    [recid] [varchar](15) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO



CREATE TABLE [dbo].[Phone_List](
    [Manager ] [nvarchar](255) NULL,
    [SalesCode] [nvarchar](255) NULL,
    [Email] [nvarchar](255) NULL,
    [PayrollCode] [nvarchar](255) NULL,
    [Mobile] [nvarchar](255) NULL,
    [FName] [nchar](20) NULL,
    [idd] [tinyint] NULL,
    [OD] [varchar](20) NULL,
    [WeeklyBudget]  AS ([dbo].[SumIt]([manager]))
) ON [PRIMARY]

阅读 190

收藏
2021-05-05

共1个答案

一尘不染

你可以用你的查询到这样的(它的功能 HAS 返回一个值):

CREATE FUNCTION dbo.SumIt(@Key1 varchar(max))
returns float
as
begin
  return (select sum(UTOTALWBUD) from
   CONTACT1 inner join
   CONTACT2 on
   CONTACT1.ACCOUNTNO=CONTACT2.ACCOUNTNO
   where KEY1=@key1
   group by KEY3)
END

并使用此函数代替calc字段-像这样:

alter table ManagerTaLog add WeeklyBudget as dbo.SumIt(Key1)

笔记

这将是诸如此类查询的性能杀手:

select * from ManagerTaLog

您应该以这种方式更改函数,即接受 NOT varchar值,但是NVARCHAR(255)-与Manager列的类型相同。试试吧。

2021-05-05