Solved

Create a sql function that Add two value from two joint table and place it in the master table

Posted on 2014-10-29
7
108 Views
Last Modified: 2014-11-06
I have the following table:   and I would like to write a function  that take the value  LocId Int Foreign key to LocTable and
DeptId Int Foreign key to DeptTable and add these two value such as A + ' ' + B and insert this value in a new column table.
([dbo].[AddTwoValuesPhone]([Location],[Department]))

Open in new window

USE [ztools]
GO
/****** Object:  UserDefinedFunction [dbo].[AddTwoValuesPhone]    Script Date: 10/29/2014 16:39:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<GUYON, Yves>
-- Create date: <200141027, ,>
-- Description:	<Phone Book, ,>
-- =============================================
ALTER FUNCTION [dbo].[AddTwoValuesPhone](@Location NVARCHAR(100),@Department NVARCHAR(150))

RETURNS NVARCHAR(150)
AS
 BEGIN
        DECLARE @Result NVARCHAR(150)
        SET @Result = @Location + ' - ' +  @Department   
        RETURN 
        (

            @Result
        )
    END

Open in new window



Client Table
ClientID Int
Name NVARCHAR(100)
LastName  NVARCHAR(100
LocId Int Foreign key to LocTable
DeptId Int Foreign key to DeptTable
LocId   This should be a value from the following table

DeptTable
Id
Name

LocTable
Id
Name
0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40411860
What is a "new column table" and why is a view not sufficient?
0
 

Author Comment

by:yguyon28
ID: 40411865
Never done a view... No I just want the data to be placed back in a column in the client Table
0
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40412060
CREATE VIEW myFirstView 
AS 
    SELECT *, Location + ' ' + Department AS AddTwoValuesPhone 
    FROM [Client Table];

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:yguyon28
ID: 40413052
Thank you and how do I call this view to get populated from my table column?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40413156
Use BOL: CREATE VIEW (Transact-SQL)

Creates a virtual table whose contents [..]

So it's a simple

SELECT *
FROM myFirstView ;

Open in new window

0
 

Author Comment

by:yguyon28
ID: 40426113
I've requested that this question be closed as follows:

Accepted answer: 0 points for yguyon28's comment #a40413052
Assisted answer: 250 points for ste5an's comment #a40412060
Assisted answer: 250 points for ste5an's comment #a40413156

for the following reason:

It work
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40421616
So you used a view or something else?
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question