Conversion failed when converting the varchar value ‘ and f.FUNCTION_ID= CAST(‘ to data type int.

huangapple go评论75阅读模式
英文:

Conversion failed when converting the varchar value ' and f.FUNCTION_ID= CAST(' to data type int

问题

我正在尝试创建一个从MS SQL获取数据的过程。在这里,我遇到了错误。

<b>Msg 245, Level 16, State 1, Procedure GET_FUNCTIONS, Line 15 [Batch Start Line 33]
Conversion failed when converting the varchar value ' and f.FUNCTION_ID in '' to data type int.</b>

CREATE procedure [dbo].[GET_FUNCTIONS]
@Function_Id int,
@EntityId int,
@OrderBy varchar(10)
as
begin
declare @Sql nvarchar(max)
if(@Function_Id=0 AND @EntityId=0)
set @Sql = 'select
f.Function_Code,f.FUNCTION_ID,f.EntityId,be.ENTITY_SHORT_NAME,f.FUNCTION_NAME,
f.FUNCTION_DESC,f.CREATED_DATE,f.MODIFIED_DATE from FUNCTIONS f 
inner join BusinessEntity be on f.EntityId=be.ID
WHERE f.ACTIVE=1 '

if(@Function_Id!=0)
set @Sql += ' and f.FUNCTION_ID in ''' + @Function_Id + ''''
if(@EntityId!=0)
set @Sql += ' and f.EntityId = cast('+convert(varchar(12) ,@EntityId)+') '

set @Sql += ' order by FUNCTION_ID '+ @OrderBy
print @Sql
end
GO
英文:

I am trying to make the procedure where I can get the data from MS SQL. Here I am getting the error

<b>Msg 245, Level 16, State 1, Procedure GET_FUNCTIONS, Line 15 [Batch Start Line 33]
Conversion failed when converting the varchar value ' and f.FUNCTION_ID in '' to data type int.</b>

    CREATE procedure [dbo].[GET_FUNCTIONS]
    @Function_Id int,
    @EntityId int,
    @OrderBy varchar(10)
    as
    begin
    declare @Sql nvarchar(max)
    if(@Function_Id=0 AND @EntityId=0)
    set @Sql = &#39;select                                                        
    f.Function_Code,f.FUNCTION_ID,f.EntityId,be.ENTITY_SHORT_NAME,f.FUNCTION_NAME,
    f.FUNCTION_DESC,f.CREATED_DATE,f.MODIFIED_DATE from FUNCTIONS f 
    inner join BusinessEntity be on f.EntityId=be.ID
    WHERE f.ACTIVE=1 &#39;

    if(@Function_Id!=0)
    set @Sql += &#39; and f.FUNCTION_ID in &#39;&#39;&#39; + @Function_Id + &#39;&#39;&#39;&#39;
    if(@EntityId!=0)
    set @Sql += &#39; and f.EntityId = cast(&#39;+convert(varchar(12) ,@EntityId)+&#39;) &#39;

    set @Sql += &#39; order by FUNCTION_ID &#39;+ @OrderBy
    print @Sql
    end
    GO

答案1

得分: 1

我已经自己找到了答案。

我已删除“if(@Function_Id=0 AND @EntityId=0)”并将@Function_Id和@EntityId的数据类型更改为varchar(100),因此解决了所有错误。

英文:

Thanks everyone for your answers. I have got the answer by my own.

I have removed if(@Function_Id=0 AND @EntityId=0) and changed the data type of @Function_Id and @EntityId to varchar(100), so solved all errors.

答案2

得分: 0

以下是翻译好的部分:

我在这里做了一些猜测,并添加了一些关于它们的注释,但这应该是你需要的内容。首先,我确保参数化了这些值,同时还验证了并引用了 `@OrderBy` 的值:

    CREATE PROCEDURE [dbo].[GET_FUNCTIONS] @Function_Id int =NULL, --而不是传递 0 作为 "选择全部" 使用 NULL
                                           @EntityId int = NULL, --而不是传递 0 作为 "选择全部" 使用 NULL
                                           @OrderBy nvarchar(4) --猜测这是指 ASC 还是 DESC?
    AS
    BEGIN
    
        IF @OrderBy NOT IN (N'ASC', 'DESC')
            --错误不是 ASC 或 DESC
            THROW 51473, N'@OrderBy 只能具有值 'ASC' 或 'DESC'。',16; --51473 是一个随机的错误编号;你可以使用适合你的环境的错误编号。
        
        DECLARE @SQL nvarchar(MAX),
                @CRLF nchar(2) = NCHAR(13) + NCHAR(10);
    
        SET @SQL = N'SELECT f.Function_Code,' + + @CRLF +
                   N'       f.FUNCTION_ID,' + + @CRLF +
                   N'       f.EntityId,' + + @CRLF +
                   N'       be.ENTITY_SHORT_NAME,' + + @CRLF +
                   N'       f.FUNCTION_NAME,' + + @CRLF +
                   N'       f.FUNCTION_DESC,' + + @CRLF +
                   N'       f.CREATED_DATE,' + + @CRLF +
                   N'       f.MODIFIED_DATE' + + @CRLF +
                   N'FROM FUNCTIONS f' + + @CRLF +
                   N'     INNER JOIN BusinessEntity be ON f.EntityId = be.ID' + + @CRLF +
                   N'WHERE f.ACTIVE = 1';
    
        IF (@Function_Id IS NOT NULL)
            SET @SQL = @SQL + @CRLF + N'  AND f.FUNCTION_ID = @Function_Id';
    
        IF (@EntityId IS NOT NULL)
            SET @SQL = @SQL + @CRLF + N'  AND f.EntityId = @EntityId';
    
        SET @SQL = @SQL + @CRLF +
                   N'ORDER BY F.FUNCTION_ID ' + QUOTENAME(@OrderBy) + N';';

这是你提供的 SQL 存储过程的翻译部分。如果需要更多帮助,请随时提出。

英文:

I'm, making a few guesses here, which I add some comments about, however, this should be what you are after. I firstly ensure that I parametrise the values, but also validate and quote the value of @OrderBy:

CREATE PROCEDURE [dbo].[GET_FUNCTIONS] @Function_Id int =NULL, --Instead of passing 0as &quot;select all&quot; use NULL
@EntityId int = NULL, --Instead of passing 0as &quot;select all&quot; use NULL
@OrderBy nvarchar(4) --guessing this is mean to be ASC or DESC?
AS
BEGIN
IF @OrderBy NOT IN (N&#39;ASC&#39;,&#39;DESC&#39;)
--Error is not ASC or DESC
THROW 51473, N&#39;@OrderBy can only have a value of &#39;&#39;ASC&#39;&#39; or &#39;&#39;DESC&#39;&#39;.&#39;,16; --51473 was a random error number; you can use one that is appropriate for your environment.
DECLARE @SQL nvarchar(MAX),
@CRLF nchar(2) = NCHAR(13) + NCHAR(10);
SET @SQL = N&#39;SELECT f.Function_Code,&#39; + + @CRLF +
N&#39;       f.FUNCTION_ID,&#39; + + @CRLF +
N&#39;       f.EntityId,&#39; + + @CRLF +
N&#39;       be.ENTITY_SHORT_NAME,&#39; + + @CRLF +
N&#39;       f.FUNCTION_NAME,&#39; + + @CRLF +
N&#39;       f.FUNCTION_DESC,&#39; + + @CRLF +
N&#39;       f.CREATED_DATE,&#39; + + @CRLF +
N&#39;       f.MODIFIED_DATE&#39; + + @CRLF +
N&#39;FROM FUNCTIONS f&#39; + + @CRLF +
N&#39;     INNER JOIN BusinessEntity be ON f.EntityId = be.ID&#39; + + @CRLF +
N&#39;WHERE f.ACTIVE = 1&#39;;
IF (@Function_Id IS NOT NULL)
SET @SQL = @SQL + @CRLF + N&#39;  AND f.FUNCTION_ID = @Function_Id&#39;
IF (@EntityId IS NOT NULL)
SET @SQL = @SQL + @CRLF + N&#39;  AND f.EntityId = @EntityId&#39;
SET @SQL = @SQL + @CRLF +
N&#39;ORDER BY F.FUNCTION_ID &#39; + QUOTENAME(@OrderBy) + N&#39;;&#39;
--PRINT @SQL; --Uncomment for debugging.
EXEC sp_executesql @SQL, N&#39;@Function_Id int, @EntityId int&#39;, @Function_Id, @EntityId;
END;
GO

huangapple
  • 本文由 发表于 2020年1月3日 21:24:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定