Create Table in SQL Server 2008

Asked

Viewed 79 times

-2

There is possibility to create a table of CAIXA that, for example, when placing the CODIGO of the product, pull the DESCRIÇÃO automatic, being in the table of PRODUTOS??

  • You want to avoid joins?

  • You can do joins

  • Depending on the context you can create a view to simplify certain queries, and of course you will need joins in its implementation.

1 answer

1

One way to achieve this is by using a Computed Column with a function, and this function will perform a select on the table Produtos Example:

CREATE FUNCTION dbo.getDescricaoProduto(@produtoID int)
RETURNS varchar(50)
AS
BEGIN
    DECLARE @retorno varchar(50)
    set @retorno =(Select Descricao from Produtos where ProdutoId = @produtoID)
    RETURN @retorno
END

ALTER TABLE dbo.Caixa ADD
    ProdutoDescricao AS dbo.getDescricaoProduto(ProdutoId)
GO

See answer on Soen: https://stackoverflow.com/questions/6867047/sql-server-computed-column-select-from-another-table

Browser other questions tagged

You are not signed in. Login or sign up in order to post.