Connecting Tech Pros Worldwide Help | Site Map

sql squeries

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 20th, 2008, 09:55 AM
mo/-/sin
Guest
 
Posts: n/a
Default 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.............

  #2  
Old August 20th, 2008, 11:15 AM
Andrew Morton
Guest
 
Posts: n/a
Default Re: sql squeries

mo/-/sin wrote:
Quote:
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.

http://balajiramesh.wordpress.com/20...l-server-2005/

Andrew


  #3  
Old August 20th, 2008, 12:35 PM
Plamen Ratchev
Guest
 
Posts: n/a
Default 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
http://www.SQLStudio.com
  #4  
Old August 20th, 2008, 12:35 PM
Plamen Ratchev
Guest
 
Posts: n/a
Default 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
http://www.SQLStudio.com
  #5  
Old August 20th, 2008, 05:05 PM
mo/-/sin
Guest
 
Posts: n/a
Default Re: sql squeries

On Aug 20, 5:28*pm, Plamen Ratchev <Pla...@SQLStudio.comwrote:
Quote:
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.com
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...........
  #6  
Old August 20th, 2008, 08:05 PM
Plamen Ratchev
Guest
 
Posts: n/a
Default 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
http://www.SQLStudio.com
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.