SQL问题:同一列中的一列

时间:2011-02-08 09:44:23

标签: sql sql-server

HY! 我有2个表,每个表都有一个列日期,我需要创建一个表,其中包含来自所有2个表的信息,其中列日期我希望从3个表中获取,但是在同一列中 我尝试了以下代码,但没有工作

CREATE FUNCTION dbo.GetContactInformation(@id int)
RETURNS @retActivityInformation TABLE 
(
ClientID int NOT NULL,
ActivityDate datetime NULL,
Tipe nvarchar(50) NULL,
Number nvarchar(50) NULL,
Value int NULL,
Statu nvarchar(50) NULL,
PRIMARY KEY CLUSTERED (clientID ASC)

) AS 
BEGIN
    DECLARE 
        @ClientID int,
        @ActivityDate datetime, 
        @Tip nvarchar(50), 
        @Number nvarchar(50),
        @Value int,
        @Statu nvarchar(50);
       SELECT 
        @ClientID = ClientID, 
        @ActivityDate = ActivityDate, 
        @Number = Number,
        @Value = Value,
        @Statu = Statu
    FROM Fa,Pay 
    WHERE ID = @id;
    SET @ActivityDate = 
        CASE 
            WHEN EXISTS(SELECT Fa.DataEmitere FROM Fa AS e 
                WHERE e.ID = @id) 
                THEN 'Fa'           
            WHEN EXISTS(SELECT Pay.Data FROM Pay AS bec
                WHERE bec.ID = @id) 
                THEN 'Pay'            
        END;

    IF @id IS NOT NULL 
    BEGIN
        INSERT @retActivityInformation
        SELECT @clientID, @ActivityDate,  @Number, @Value,@Statu;
    END;

    RETURN;
END;

3 个答案:

答案 0 :(得分:0)

只需在字段前添加数据库名称。我假设你的实际意思是ActivityDate。如果您想使用此字段进行SELECT / INSERT,则需要使用FaPay作为前缀,以便Fa.ActivityDatePay.ActivityDate

如果这不是该字段,那么我们需要更多信息。

答案 1 :(得分:0)

通过指定表名来使用该列,如下所示: -

CREATE FUNCTION dbo.GetContactInformation(@id int)
RETURNS @retActivityInformation TABLE 
(
ClientID int NOT NULL,
ActivityDate datetime NULL,
Tipe nvarchar(50) NULL,
Number nvarchar(50) NULL,
Value int NULL,
Statu nvarchar(50) NULL,
PRIMARY KEY CLUSTERED (clientID ASC)

) AS 
BEGIN
    DECLARE 
        @ClientID int,
        @ActivityDate datetime, 
        @Tip nvarchar(50), 
        @Number nvarchar(50),
        @Value int,
        @Statu nvarchar(50);
       SELECT 
        @ClientID = ClientID, 
        @ActivityDate = Fa.ActivityDate, 
        @Number = Number,
        @Value = Value,
        @Statu = Statu
    FROM Fa,Pay 
    WHERE ID = @id;
    SET @ActivityDate = 
        CASE 
            WHEN EXISTS(SELECT Fa.DataEmitere FROM Fa AS e 
                WHERE e.ID = @id) 
                THEN 'Fa'           
            WHEN EXISTS(SELECT Pay.Data FROM Pay AS bec
                WHERE bec.ID = @id) 
                THEN 'Pay'            
        END;

    IF @id IS NOT NULL 
    BEGIN
        INSERT @retActivityInformation
        SELECT @clientID, @ActivityDate,  @Number, @Value,@Statu;
    END;

    RETURN;
END;

答案 2 :(得分:0)

请参阅此处的中间部分:

CREATE FUNCTION dbo.GetContactInformation(@id int)
RETURNS @retActivityInformation TABLE 
(
ClientID int NOT NULL,
ActivityDate datetime NULL,
Tipe nvarchar(50) NULL,
Number nvarchar(50) NULL,
Value int NULL,
Statu nvarchar(50) NULL,
PRIMARY KEY CLUSTERED (clientID ASC)

) AS 
BEGIN
    DECLARE 
        @ClientID int,
        @ActivityDate datetime, 
        @Tip nvarchar(50), 
        @Number nvarchar(50),
        @Value int,
        @Statu nvarchar(50);
       SELECT 
        @ClientID = ClientID, 
        @ActivityDate = ActivityDate, 
        @Number = Number,
        @Value = Value,
        @Statu = Statu
    FROM Fa,Pay 
    WHERE ID = @id;
    SET @ActivityDate = ISNULL(
        (SELECT top 1 Fa.DataEmitere FROM Fa AS e WHERE e.ID = @id),
        (SELECT top 1 Pay.Data FROM Pay AS bec WHERE bec.ID = @id))

    IF @id IS NOT NULL 
    BEGIN
        INSERT @retActivityInformation
        SELECT @clientID, @ActivityDate,  @Number, @Value,@Statu;
    END;

    RETURN;
END;

基本上,不是测试数据是否只是为了获取字段名称,而是直接获取数据。