مشکل در استفاده از stored procedure
سلام
من میخام از یه stored procedure برای وارد کردن اطلاعات به جدول مربوطه استفاده کنم.
با این کد :
CREATE PROCEDURE [dbo].[usp_insert_Master]
@table varchar(50) ,
@onvan nvarchar(200),
@kholase nvarchar(600),
@matneasli nvarchar(MAX),
@datetime varchar(50),
@Image nvarchar(100)
AS
INSERT INTO @table
(Onvan , Kholase , MatneAsli , DateTime , Image)
VALUES(@Onvan , @Kholase , @MatneAsli , @DateTime , @Image)
ولی موقع اجرا از @table ایراد می گیره.
Msg 1087, Level 15, State 2, Procedure usp_insert_Master, Line 9
Must declare the table variable "@table".
لطفاً راهنمایی کنید.
نقل قول: مشکل در استفاده از stored procedure
نام جدول نمی تواند متغیر باشد.
برای اینکه بتوانین نام جدول مورد نظر هم متغیر انتخاب کنین می توانین از Dynamic SQL استفاده کنین (دستورات execute و SP)
نقل قول: مشکل در استفاده از stored procedure
سلام
دوست عزیز مشکلم در اجرای sp حل شد ولی حالا یه مشکل دیگه به وجود اومده .
وقتی که sp رو با داده های ورودی اجرا می کنم error می گیره.
این کد sp :
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[usp_insert_master]
@table nvarchar(50),
@onvan nvarchar(200),
@kholase nvarchar(1000),
@matneasli nvarchar(max),
@datetime nvarchar(200),
@Image nvarchar(300)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @command nvarchar(Max)
SET @command =
'INSERT INTO ' + @table +
' (onvan , kholase , matneasli , datetime , image)' +
' VALUES(' + @onvan + ' , ' + @kholase +' , ' + @matneasli +' , '+ @datetime + ' , ' + @Image +')'
EXEC (@command)
END
و این هم پیغام خطا
Msg 128, Level 15, State 1, Line 1
The name "سلام" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
اگر ممکنه راهنمایی کنید.
نقل قول: مشکل در استفاده از stored procedure
سلام،
مشکل بر می گردد به نقل قول.
وقتی می خواهین یک رشته را در یک جدول درج کنین از [] یا '' استفاده می کنین در اینجا هم باید همین کار را انجام دهین یعنی قبل و بعد رشته هایی که می خواهین درج کنین از علامت نقل قول استفاده کنین.
از دو راه می توانین این کار را انجام دهین یکی اینکه مستقیما از تابع نقل قول استفاده کنین یا اینکه از چهارتا تک کتیشتن استفاده کنین.
روش چهار کتیشن:
declare @a char(1), @b char(3)
set @a='a'
execute('print '+''''+@a+'''')
--output: a
روش استفاده از تابع نقل قول (توجه کنین که در SQL Server 2008 من سعی کردم از این تابع در دستور exec استفاده کنم ولی جواب ندارد)
set @b=Quotename(@a,'')
print @b --output: [a]
--execute('print '+Quotename(@a,''))
نقل قول: مشکل در استفاده از stored procedure
دوست عزیز ، از راه اول که فرمودید یعنی چهار تا تک کتیشن استفاده کردم و جواب داد.
از راهنماییتون سپاسگذارم .
موفق باشید
----------------------------------------------------------------------
خدا جبران تمام نداشته های من است . ( دکتر شریعتی )
نقل قول: مشکل در استفاده از stored procedure
نقل قول:
نوشته شده توسط
meysampaknahad
دوست عزیز ، از راه اول که فرمودید یعنی چهار تا تک کتیشن استفاده کردم و جواب داد.
از راهنماییتون سپاسگذارم .
موفق باشید
----------------------------------------------------------------------
خدا جبران تمام نداشته های من است . ( دکتر شریعتی )
استفاده از Procedure مربوط به Dynamic SQL یعنی sp_executesql عملکرد (Performance) بهتری دارد.
پیشنهاد می کنم کمی در این زمینه تحقیق کنین.