Welcome to Sign in | Help

Re: calculul unei valori in functie de alta

  •  10-06-2006, 10:06 PM

    Re: calculul unei valori in functie de alta

    Sunt mai multe solutii printre care folosirea unei coloane calculate.
    Un exemplu folosind o coloana calculata este urmatorul:

    USE tempdb;
    GO
    CREATE TABLE myTable(ID INT NOT NULL PRIMARY KEY,
          PRET_FARA_TVA DEC(19,2),
          PRET_CU_TVA AS PRET_FARA_TVA * 1.19)
    GO
    INSERT INTO myTable(ID,PRET_FARA_TVA) VALUES (1,100);
    GO
    SELECT * FROM myTable;
    GO
    -- Output
    -- ID PRET_FARA_TVA PRET_CU_TVA 
    --  1 100.00        119.00

    Acelasi exemplu un pic mai elegant, folosind o functie:

    USE tempdb;
    GO
    CREATE FUNCTION dbo.fnGetTVA(@PRET DEC(19,2))
    RETURNS DEC(19,2)
    AS
    BEGIN
    RETURN @PRET * 1.19
    END
    GO
    CREATE TABLE myTable2(ID INT NOT NULL PRIMARY KEY,
                PRET_FARA_TVA DEC(19,2),
                PRET_CU_TVA AS dbo.fnGetTVA(PRET_FARA_TVA))
    GO
    INSERT INTO myTable2(ID,PRET_FARA_TVA) VALUES (1,100);
    GO
    SELECT * FROM myTable2;
    GO
    -- Output
    -- ID PRET_FARA_TVA PRET_CU_TVA 
    --  1 100.00        119.00 

    Am dat exemplul folosind baza de date tempdb pentru a nu fi necesara stergerea obiectelor (tempdb fiind recreata la fiecare restart al serverului).


    Cristian Andrei Lefter, SQL Server MVP
    MCT, MCSA, MCDBA, MCAD, MCSD .NET,
    MCTS, MCITP - Database Administrator SQL Server 2005
    http://sqlserver.ro
View Complete Thread
Powered by Community Server (Commercial Edition), by Telligent Systems