1

I tried to assign the values to a view using parameters but I'm getting the error:

'ALTER VIEW' must be the first statement in a query batch.

When using GO statement, its unable to access the variables.

How can I assign parameters to a view.

USE [DBNAME]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CA_Removal varchar(max)
SET @CA_Removal=(SELECT value FROM spider.Configuration WHERE id='Removal')
GO

ALTER view [spider3].[View] WITH SCHEMABINDING
as
select
asa.id,
ase.common,
from
[spider].Activity asa
inner join
[spider].External ase
on
asa.primaryKey = ase.owner
where
asa.type = TRY_CONVERT(int, @CA_Removal)

When asa.type = 10 it works properly.

9
  • You cannot use a local variable as some sort of kludge to parameterize a view. But you don't appear to need a parameter. Just change the where clause to where asa.type = (select value FROM spider.Configuration WHERE id='Removal'); Commented Aug 4, 2021 at 6:00
  • @Zhorov im getting error as -->Must declare the scalar variable "@CA_Removal". Commented Aug 4, 2021 at 6:01
  • 3
    You can't use a parameter in a view. If you can't inline the 10 sub-queries (which is perfectly reasonable to do using cross apply), you need a table valued function. You need to ensure your question accurately reflects the problem you are trying to solve simplify yes, but don't oversimplify. Commented Aug 4, 2021 at 6:12
  • 2
    @Codeninja as I said above You can't use a parameter in a view - never - ever. The fact that you have a GO between your parameter definition and your view definition means they are in 2 completely separate batches and have no knowledge of each other. Commented Aug 4, 2021 at 6:39
  • 1
    Check my answear - there is a method how to use parameter in view Commented Aug 4, 2021 at 12:45

3 Answers 3

2

You don't want a view. You want a table-valued function:

create function spider3.udf_spider (
    @CA_Removal varchar(max)
) returns table
as return(select asa.id, ase.common
           from [spider].Activity asa join
                [spider].External ase
                on asa.primaryKey = ase.owner
           where asa.type = TRY_CONVERT(int, @CA_Removal)
          );

It seems really awkward to pass in a string that you just convert to an integer. I would strongly recommend that you pass in the value as an integer:

create function spider3.udf_spider (
    @CA_Removal int
) returns table
as return(select asa.id, ase.common
           from [spider].Activity asa join
                [spider].External ase
                on asa.primaryKey = ase.owner
           where asa.type = @CA_Removal
          );
Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned by others in the comments, a view cannot have parameters. Furthermore, the two batches are entirely separate, and cannot reference variables from one to the next anyway.

You have two options:

  • Create the view with a join
CREATE OR ALTER view [spider3].[View] WITH SCHEMABINDING
as
select
  asa.id,
  ase.common,
from
  [spider].Activity asa
inner join
  [spider].External ase
  on asa.primaryKey = ase.owner
where asa.type = TRY_CONVERT(int,
    (SELECT value FROM spider.Configuration WHERE id='Removal')
);

GO
  • Create an inline Table Valued Function
CREATE OR ALTER FUNCTION [spider3].[iTVF]
(@typeId int)
RETURNS TABLE
WITH SCHEMABINDING
AS RETURN (

select
  asa.id,
  ase.common,
from
  [spider].Activity asa
inner join
  [spider].External ase
  on asa.primaryKey = ase.owner
where asa.type = @typeId

);

GO

Comments

1

Normally views are not parameterized. But you could always inject some parameters. For example using session context:

CREATE VIEW my_view
AS
SELECT *
FROM tab
WHERE num = SESSION_CONTEXT(N'my_num');

Invocation:

EXEC sp_set_session_context 'my_num', 1; 
SELECT * FROM my_view;

And another:

EXEC sp_set_session_context 'my_num', 2; 
SELECT * FROM my_view;

DbFiddleDemo

1 Comment

@ThorstenKettner Not me, but seems to be a hack to me, why not just use a TVF like you're supposed to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.