using TVP

I am trying to familiarize myself with the new TVP ability in server 2008
I have created a test file with the following creation codes
run in the order which I have placed them
The last is of course executing one of the SP

This is the error which I get
(0 row(s) affected)
Msg 208, Level 16, State 1, Procedure Table_Test, Line 5
Invalid object name 'newbudget'.

I know this is something simple but can not see it.

CREATE TABLE [dbo].[project](
	[project_no] [int] NOT NULL,
	[project_name] [nchar](10) NOT NULL,
	[budget] [float] NOT NULL
) ON [PRIMARY]

GO

CREATE TYPE [dbo].[newbudget] AS TABLE(
    [project_no] [int] NOT NULL,
	[budget] [float] NOT NULL,
	[rate] [int] NOT NULL

)
GO
CREATE   FUNCTION  [dbo].[nRate](@proj_no int)
	RETURNS int
AS
BEGIN
      DECLARE @rate int
      set @rate = case when (@proj_no % 2)=0 then 2 else 1 end
      RETURN @rate
END
GO

table AS newbudget READONLY)
AS
DECLARE @avgBudget FLOAT
SELECT @avgBudget = AVG(budget) FROM newbudget
PRINT 'The average budget is' + @avgBudget
GO

CREATE PROC Test_budget_call
AS
DECLARE @nbudget AS newbudget

INSERT INTO @nbudget ([project_no],[budget],[rate])

SELECT project_no,budget,dbo.nRate(project_no) AS rate

FROM project

EXEC Table_Test @nbudget

GO

EXEC Test_Budget_call
go

Open in new window

TagomAsked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
Fixed script
CREATE TABLE [dbo].[project](
	[project_no] [int] NOT NULL,
	[project_name] [nchar](10) NOT NULL,
	[budget] [float] NOT NULL
) ON [PRIMARY]

GO

CREATE TYPE [dbo].[newbudget] AS TABLE(
    [project_no] [int] NOT NULL,
	[budget] [float] NOT NULL,
	[rate] [int] NOT NULL

)
GO
CREATE   FUNCTION  [dbo].[nRate](@proj_no int)
	RETURNS int
AS
BEGIN
      DECLARE @rate int
      set @rate = case when (@proj_no % 2)=0 then 2 else 1 end
      RETURN @rate
END
GO
create proc table_test (
@table AS newbudget READONLY)
AS
DECLARE @avgBudget FLOAT
SELECT @avgBudget = AVG(budget) FROM @table
PRINT 'The average budget is' + convert(varchar,@avgBudget)
GO

CREATE PROC Test_budget_call
AS
DECLARE @nbudget AS newbudget

INSERT INTO @nbudget ([project_no],[budget],[rate])

SELECT project_no,budget,dbo.nRate(project_no) AS rate

FROM project

EXEC Table_Test @nbudget

GO

EXEC Test_Budget_call
go

Open in new window

0
 
cyberkiwiCommented:
drop the AS keyword

DECLARE @nbudget newbudget
0
 
cyberkiwiCommented:
Hi there,

I pasted the whole thing into SSMS and it is saying that TABLE_Test does not exist.  It is not in the script above, can you please show what is in it?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
TagomAuthor Commented:
gives the same error
0
 
TagomAuthor Commented:
sorry missed copying some of it
Here is that code

CREATE PROC Table_Test
(@ntable AS newbudget READONLY)
AS
DECLARE @avgBudget FLOAT
SELECT @avgBudget = AVG(budget) FROM newbudget
PRINT 'The average budget is' + @avgBudget
0
 
cyberkiwiCommented:
I guessed that :)

CREATE PROC Table_Test
(@ntable AS newbudget READONLY)
AS
DECLARE @avgBudget FLOAT
SELECT @avgBudget = AVG(budget) FROM @ntable  --- use the table var here
PRINT 'The average budget is' + convert(varchar, @avgBudget)   -- can't add into to text
0
 
TagomAuthor Commented:
Perfect
Thank you for taking the time to come back and highlight the mistakes....that helps in trying to understand how to manipulate these tables
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.