我的SQL语句出错, [英] Error with my SQL statement,

查看:75
本文介绍了我的SQL语句出错,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:)

我试图在SQL SERVER 2008 R2中的存储过程中使用IF语句.

但是有一些我不知道如何解决的错误.

有什么帮助吗?

这是我的存储过程

Hello :)

I tried to use IF Statement within my stored procedure in SQL SERVER 2008 R2

but there are errors that I don''t know how to fix it.

any help please ?

here is my stored procedure

USE [MsaGroup_SYTS]
GO
/****** Object:  StoredProcedure [dbo].[InserNewcommentFromViewPages]    Script Date: 03/05/2012 02:08:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[InserNewcommentFromViewPages]
@_UserID Int,
@_Text NVarChar(MAX),
@_Type NVarChar(50),
@_Date DateTime,
@_ItemID Int
as
insert
into
Comments
(
Comments.[User_ID],
Comment_Text,
Comment_Type,
Comment_Date,
Comment_TypeID,
Comment_FlagIsActive,
Comment_Original
)
values
(
@_UserID,
@_Text,
@_Type,
@_Date,
@_ItemID,
'true',
'~/pages/' + @_Type + 'view.aspx?ID=' + @_ItemID + '&Image='
+ (IF (@_Type  = 'Parteners') 
BEGIN
   SELECT Partners.Partner_ImageGalleryID from Partners where Partner_ID = @_ItemID
END
ELSE
BEGIN
	IF (@_Type  = 'News') 
	BEGIN
		SELECT News.News_ImageGalleryID from News where News.News_ID = @_ItemID
	END
END
ELSE
BEGIN
	IF (@_Type  = 'Prodects') 
	BEGIN
		SELECT Products.Product_ImageGalleryID from Products where Product_ID = @_ItemID
	END
END
ELSE
BEGIN
	IF (@_Type  = 'Ads') 
	BEGIN
		SELECT Ads.Ads_ID from Ads where Ads.Ads_ID = @_ItemID
	END
END
)
+ '&Video='
+ (IF (@_Type  = 'Parteners') 
BEGIN
   SELECT Partners.Partner_VideoGalleryID from Partners where Partner_ID = @_ItemID
END
ELSE
BEGIN
	IF (@_Type  = 'News') 
	BEGIN
		SELECT News.News_VideoGalleryID from News where News.News_ID = @_ItemID
	END
END
ELSE
BEGIN
	IF (@_Type  = 'Prodects') 
	BEGIN
		SELECT Products.Product_VideoGalleryID from Products where Product_ID = @_ItemID
	END
END
ELSE
BEGIN
	IF (@_Type  = 'Ads') 
	BEGIN
		SELECT Ads.Ads_ID from Ads where Ads.Ads_ID = @_ItemID
	END
END
)
)

推荐答案

只是一个提示..

可能是您必须将int强制转换为字符串

我的意思是
just a hint..

may be you have to cast the int to string

i mean this
'~/pages/' + @_Type + 'view.aspx?ID=' + @_ItemID + '&Image='



进入这个



into this

'~/pages/' + @_Type + 'view.aspx?ID=' + cast(@_ItemID as nvarchar(max)) + '&Image='



而且我也会投放您选择的ID

注意:您确定select返回单个值吗?



and i would cast the IDs that you select too

NOTE : are you sure that the select return a single value?


您似乎正在尝试在SELECT语句的中间使用IF? IF用于过程式Transact-SQL.对于查询中的IF功能,您需要使用CASE-
It looks like you''re attempting to use IF in the middle of the SELECT statement? IF is for procedural Transact-SQL. For IF functionality within a query, you''d need CASE - http://www.techrepublic.com/article/in-t-sql-use-casewhen-in-place-of-ifthen/5078041[^]
Scott


这篇关于我的SQL语句出错,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆