英文:
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 = '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
答案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 "select all" use NULL
@EntityId int = NULL, --Instead of passing 0as "select all" use NULL
@OrderBy nvarchar(4) --guessing this is mean to be ASC or DESC?
AS
BEGIN
IF @OrderBy NOT IN (N'ASC','DESC')
--Error is not ASC or DESC
THROW 51473, N'@OrderBy can only have a value of ''ASC'' or ''DESC''.',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'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';'
--PRINT @SQL; --Uncomment for debugging.
EXEC sp_executesql @SQL, N'@Function_Id int, @EntityId int', @Function_Id, @EntityId;
END;
GO
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论