sql squeries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mo/-/sin

    sql squeries

    i want to make a table in sql server2005 with 3 columns the data type
    of first two column is int.. i want that the value in column 1 should
    be multiplied
    by the value in column 2 and the total should appear in column 3.. is
    it possible....... ......
  • Andrew Morton

    #2
    Re: sql squeries

    mo/-/sin wrote:
    i want to make a table in sql server2005 with 3 columns the data type
    of first two column is int.. i want that the value in column 1 should
    be multiplied
    by the value in column 2 and the total should appear in column 3.. is
    it possible....... ......
    It sounds like you want a computed column, e.g.

    Using Persited Columns in SQL Server 2005 via t-sql and sql server management studio


    Andrew


    Comment

    • Plamen Ratchev

      #3
      Re: sql squeries

      In addition to using a computed column, you can create a view to perform
      the calculation and use the view as needed:

      CREATE TABLE Foo (
      keycol INT PRIMARY KEY,
      col1 INT,
      col2 INT);

      INSERT INTO Foo VALUES(1, 3, 5);

      GO

      CREATE VIEW Bar (col1, col2, col3)
      AS
      SELECT col1, col2, col1 * col2
      FROM Foo;

      GO

      SELECT col1, col2, col3
      FROM Bar;


      Plamen Ratchev

      Comment

      • Plamen Ratchev

        #4
        Re: sql squeries

        Actually a better example for the view should have been:

        CREATE VIEW Bar (keycol, col1, col2, col3)
        AS
        SELECT keycol, col1, col2, col1 * col2
        FROM Foo;


        Plamen Ratchev

        Comment

        • mo/-/sin

          #5
          Re: sql squeries

          On Aug 20, 5:28 pm, Plamen Ratchev <Pla...@SQLStud io.comwrote:
          In addition to using a computed column, you can create a view to perform
          the calculation and use the view as needed:
          >
          CREATE TABLE Foo (
            keycol INT PRIMARY KEY,
            col1 INT,
            col2 INT);
          >
          INSERT INTO Foo VALUES(1, 3, 5);
          >
          GO
          >
          CREATE VIEW Bar (col1, col2, col3)
          AS
          SELECT col1, col2, col1 * col2
          FROM Foo;
          >
          GO
          >
          SELECT col1, col2, col3
          FROM Bar;
          >
          Plamen Ratchevhttp://www.SQLStudio.c om
          buddy.. here you are passing three value i want that i shall insert
          only two values and the product of the two values should appear in
          third column.......
          and moreover i m not talking about the select statement...... ..... i
          want to enter that value in the table.......... .

          Comment

          • Plamen Ratchev

            #6
            Re: sql squeries

            Did you try the example? The insert statement has 3 values because it
            inserts the key column too. The view will calculate col3 automatically.


            Plamen Ratchev

            Comment

            Working...