Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Function Not Working Expand / Collapse
Author
Message
Posted Monday, January 28, 2013 10:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 10, 2013 10:56 AM
Points: 11, Visits: 76
How to create a computed column based on the Title nvarchar(max) column of another field: I want to make Title in table 1 a computed column from table2 based on docId=DocId. I keep trying to add it as computed column in SSMS and it wont let me.

Table1

AssetId Title DocId
1 10
========================


Table2

DocId Title
=========================
5 Disaster
10 Recovery
15 Pending


CREATE FUNCTION dbo.GetValue (@DocId INT)
RETURNS TABLE
AS
RETURN
(SELECT T1.AssetId, T1.DocId, T2.Title
FROM Table1 T1
JOIN Table2 T2
ON T1.DocId = T2.DocId
WHERE T1.DocID = @DocId
)
GO


--I was messing with this code, but its not working, I want to be able to make Table1 Title a computed column based on Table 2 Title column and documentId

[code="sql"]
ALTER TABLE dbo.Table1
ADD Title AS dbo.GetValue(Title)

I get errors.
Post #1412557
Posted Monday, January 28, 2013 10:56 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 4:48 PM
Points: 577, Visits: 4,163
The way you have your GetValue function built, you need to put the DocID in as your arguement in the function, not the title.
Post #1412563
Posted Monday, January 28, 2013 11:06 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 2:33 PM
Points: 8,620, Visits: 8,261
mail4bbt (1/28/2013)
How to create a computed column based on the Title nvarchar(max) column of another field: I want to make Title in table 1 a computed column from table2 based on docId=DocId. I keep trying to add it as computed column in SSMS and it wont let me.

Table1

AssetId Title DocId
1 10
========================


Table2

DocId Title
=========================
5 Disaster
10 Recovery
15 Pending


CREATE FUNCTION dbo.GetValue (@DocId INT)
RETURNS TABLE
AS
RETURN
(SELECT T1.AssetId, T1.DocId, T2.Title
FROM Table1 T1
JOIN Table2 T2
ON T1.DocId = T2.DocId
WHERE T1.DocID = @DocId
)
GO


--I was messing with this code, but its not working, I want to be able to make Table1 Title a computed column based on Table 2 Title column and documentId

[code="sql"]
ALTER TABLE dbo.Table1
ADD Title AS dbo.GetValue(Title)

I get errors.


It seems you are new around here. Welcome!!!

Posting things like "I get errors." is really not very helpful. What are the errors?

I see far more wrong what you have here than roryp suggested. You do in fact have a datatype mismatch but there are far more serious issues with the design of what you are doing. Your function returns a table with 3 columns and you are trying to put that into a single column. This is never going to work like that.

What are you trying to do here?

You should probably take a look at the first link in my signature for best practices when posting questions.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1412567
Posted Monday, January 28, 2013 11:09 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 10, 2013 10:56 AM
Points: 11, Visits: 76
Ok, Thanks, I new here and to sql.

I am trying to make Title in table 1 a computed column that will get updated every time the title column in table 2 gets updated.
Post #1412568
Posted Monday, January 28, 2013 11:17 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 2:33 PM
Points: 8,620, Visits: 8,261
mail4bbt (1/28/2013)
Ok, Thanks, I new here and to sql.

I am trying to make Title in table 1 a computed column that will get updated every time the title column in table 2 gets updated.


We can help but you have to first help us. Please read the link I previously suggested. Without something to work with there is little chance you will get correct answers.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1412573
Posted Monday, January 28, 2013 11:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 10, 2013 10:56 AM
Points: 11, Visits: 76
CREATE TABLE [dbo].[Asset](
[AssetId] [int] NOT NULL,
[DocId] [int] NULL,
[Title] [nvarchar](max) NULL,
CONSTRAINT [PK_Asset] PRIMARY KEY CLUSTERED
(
[AssetId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

CREATE TABLE [dbo].[Doc](
[DocId] [int] NOT NULL,
[Title] [nvarchar](max) NULL,
CONSTRAINT [PK_Doc] PRIMARY KEY CLUSTERED
(
[DocId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO


INSERT INTO [Doc]([Title]) 
VALUES('Recovery');

INSERT INTO [Doc]([Title])
VALUES('Disaster');

INSERT INTO [Doc]([Title])
VALUES('Pending');

INSERT INTO [Doc]([Title])
VALUES(NULL);

INSERT INTO [Asset]([DocId], [Title])
VALUES(2, NULL);

Post #1412579
Posted Monday, January 28, 2013 11:35 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 10, 2013 10:56 AM
Points: 11, Visits: 76
I put sample values in the asset table just to see what I meant
Post #1412580
Posted Monday, January 28, 2013 12:19 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 2:33 PM
Points: 8,620, Visits: 8,261
The sample you posted won't work because there is no DocId specified on the inserts into Doc.

I need to understand the relationship between these two tables. The way you have them defined is denormalized. Do you have an Asset that can have any number of Docs? Is that what you are trying to do here?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1412600
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse