Solved Not accept Parameters (1 Viewer)

zezo2021

Member
Local time
Today, 12:14
Joined
Mar 25, 2021
Messages
381
Why does this code not accept the parameters inside SQL create view from the stored procedure?

I want to pass the value of stored procedure parameters to create a view

Code:
Create PROCEDURE MyProcedure
@ID int
AS

BEGIN
-- if object_id ('myview' ,'v') is not null
DROP VIEW IF EXISTS myview

    DECLARE @viewName VARCHAR(100) = 'MyView';
    DECLARE @viewSQL NVARCHAR(MAX) = 'CREATE VIEW ' + @viewName + ' AS SELECT * FROM Client' + 'where ID=@ID';

    EXEC sp_executesql @viewSQL;
END;
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:14
Joined
Oct 29, 2018
Messages
21,473
It looks like your string concatenation may be a bit off.
 

zezo2021

Member
Local time
Today, 12:14
Joined
Mar 25, 2021
Messages
381
AI suggestion

Code:
CREATE PROCEDURE [dbo].[MyProcedure]
@ID int
AS
BEGIN
-- if object_id ('myview' ,'v') is not null
DROP VIEW IF EXISTS myview

DECLARE @sql nvarchar(max)

SET @sql = 'CREATE VIEW dbo.myview AS
        SELECT dbo.Client.ClientName, dbo.Client.ID
        FROM client
        WHERE Client.ID = @ID;'

EXEC sp_executesql @sql, N'@ID int', @ID
END


but not work also
 

zezo2021

Member
Local time
Today, 12:14
Joined
Mar 25, 2021
Messages
381
this error appear
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'VIEW'.
 

cheekybuddha

AWF VIP
Local time
Today, 11:14
Joined
Jul 21, 2014
Messages
2,280
Just create the query in Access

It will be just as efficient as creating the view on the server.
 

zezo2021

Member
Local time
Today, 12:14
Joined
Mar 25, 2021
Messages
381
error
Conversion failed when converting the varchar value 'CREATE VIEW MyView AS SELECT * FROM Client where Client.ID=' to data type int.

I do this
DECLARE @viewSQL NVARCHAR(MAX) = 'CREATE VIEW ' + @viewName + ' AS SELECT * FROM Client' + 'where ID='+@ID;
 

sonic8

AWF VIP
Local time
Today, 12:14
Joined
Oct 27, 2015
Messages
998
Conversion failed when converting the varchar value 'CREATE VIEW MyView AS SELECT * FROM Client where Client.ID=' to data type int.
You're almost there.
The error happens because SQL Server tries to use the + operator for a mathematical addition unless all operands are of a text data type. To prevent this, you need to explicitly convert @ID to text.

Code:
... + ' where ID='+ convert(nvarchar,@ID);
--    ^^
Mind the blank I added before the Where! That addresses the other problem with your string concatenation I mentioned.
 

zezo2021

Member
Local time
Today, 12:14
Joined
Mar 25, 2021
Messages
381
You're almost there.
The error happens because SQL Server tries to use the + operator for a mathematical addition unless all operands are of a text data type. To prevent this, you need to explicitly convert @ID to text.

Code:
... + ' where ID='+ convert(nvarchar,@ID);
--    ^^
Mind the blank I added before the Where! That addresses the other problem with your string concatenation I mentioned.

👌 👌 👌 👌 👌 👌 👌

:love::love::love::love::love::love::love::love::love:
(y)(y)(y)(y)(y)(y)(y)(y)(y)(y)

best
 

Users who are viewing this thread

Top Bottom