所以我创建了一个命中Dynamics GP Vendor表的存储过程。它需要能够在不同的数据库中访问同一个表。我看到的一种可能的解决方案就是用查询编写为varchar来执行参数。以下是存储过程中实际存在的代码:
Procedure [dbo].[DGP_addVendor]
@dbName varchar(4) = NULL
,@NoteIndex numeric(19,5) = NULL
,@VENDORID char(15) = NULL
, @VENDNAME char(65) = NULL
, @DEX_ROW_ID int = NULL output
, @O_ErrorCode int = 0 output
, @ADDRESS1 char(61) = ''
, @ADDRESS2 char(61) = ''
, @ADDRESS3 char(61) = ''
, @CITY char(35) = ''
, @STATE char(29) = ''
, @ZIPCODE char(11) = ''
, @COUNTRY char(61) = ''
, @PHNUMBR1 char(21) = '' as
declare @today datetime = convert(datetime,'01/01/1900')
declare @defaultDate datetime = convert(datetime,'01/01/1900')
--declare @DEX int = null
Declare @sql varchar(MAX) = @dbName + '.dbo.zDP_PM00200SI
'''+@VENDORID+'''
,'''+@VENDNAME+'''
,'''+@VENDNAME+'''
,'''+@VENDNAME+'''
,''PRIMARY''
,''PRIMARY''
,''PRIMARY''
,''PRIMARY''
,''''
,''''
,'''+@ADDRESS1+'''
,'''+@ADDRESS2+'''
,'''+@ADDRESS3+'''
,'''+@CITY+'''
,'''+@STATE+'''
,'''+@ZIPCODE+'''
,'''+@COUNTRY+'''
,'''+@PHNUMBR1+'''
,''''
,''''
,''''
,''''
,''''
,''''
,''''
,''''
,1
,''''
,''''
,''''
,0
,1
,0
,0
,''''
,0
,0
,0
,0
,0
,''''
,''''
,''''
,''''
,0
,''''
,1
,1
,1
,1
,0
,0
,1
,1
,0
,0
,0
,''''
,'+Convert(varchar,@defaultDate)+'
,'+Convert(varchar,@defaultDate)+'
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,'+Convert(varchar,@NoteIndex)+'
,''''
,'+Convert(varchar,@today)+'
,'+Convert(varchar,@today)+'
,''''
,1
,0
,1
,''''
,''''
,0
,0
,''''
,0
,0
,0
,0
,0
,0
,'+Convert(varchar,@defaultDate)+'
,0
,''''
,''''
,0
,9
,0
,''PRIMARY''
,' + Convert(varchar,@DEX_ROW_ID) + ' out'
EXEC(@sql)
以下是我用来调用它的代码:
exec [mdpSupportServices].[dbo].[DGP_addVendor] 'LFD', @index, 'ANT0000001','Anthony Quisenberry',@row out, @error out, '8506 west Rd', '','','Louisville', 'KY', '40247', 'USA',''
它似乎没有做任何事情。如果我做错了,那么有人能指出我更好的方向吗?
答案 0 :(得分:3)
因此,此代码版本显示包含调试代码。请记住,连接空值将导致空值。
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DGP_addVendor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [DGP_addVendor]
GO
CREATE PROCEDURE [dbo].[DGP_addVendor]
@dbName VARCHAR(4) = NULL ,
@NoteIndex NUMERIC(19, 5) = NULL ,
@VENDORID CHAR(15) = NULL ,
@VENDNAME CHAR(65) = NULL ,
@DEX_ROW_ID INT = NULL OUTPUT ,
@O_ErrorCode INT = NULL OUTPUT ,
@ADDRESS1 CHAR(61) = '' ,
@ADDRESS2 CHAR(61) = '' ,
@ADDRESS3 CHAR(61) = '' ,
@CITY CHAR(35) = '' ,
@STATE CHAR(29) = '' ,
@ZIPCODE CHAR(11) = '' ,
@COUNTRY CHAR(61) = '' ,
@PHNUMBR1 CHAR(21) = ''
AS
DECLARE @today DATETIME ,
@defaultDate DATETIME ,
@sql VARCHAR(MAX)
IF @DEX_ROW_ID IS NULL
SET @DEX_ROW_ID = -1
SET @today = CONVERT(DATETIME, '01/01/1900')
SET @defaultDate = CONVERT(DATETIME, '01/01/1900')
SET @sql = 'EXEC ' + @dbName + '.dbo.zDP_PM00200SI
''' + @VENDORID + '''
,''' + @VENDNAME + '''
,''' + @VENDNAME + '''
,''' + @VENDNAME + '''
,''PRIMARY''
,''PRIMARY''
,''PRIMARY''
,''PRIMARY''
,''''
,''''
,''' + @ADDRESS1 + '''
,''' + @ADDRESS2 + '''
,''' + @ADDRESS3 + '''
,''' + @CITY + '''
,''' + @STATE + '''
,''' + @ZIPCODE + '''
,''' + @COUNTRY + '''
,''' + @PHNUMBR1 + '''
,''''
,''''
,''''
,''''
,''''
,''''
,''''
,''''
,1
,''''
,''''
,''''
,0
,1
,0
,0
,''''
,0
,0
,0
,0
,0
,''''
,''''
,''''
,''''
,0
,''''
,1
,1
,1
,1
,0
,0
,1
,1
,0
,0
,0
,''''
,' + CONVERT(VARCHAR, @defaultDate) + '
,' + CONVERT(VARCHAR, @defaultDate) + '
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,0
,' + CONVERT(VARCHAR, @NoteIndex) + '
,''''
,' + CONVERT(VARCHAR, @today) + '
,' + CONVERT(VARCHAR, @today) + '
,''''
,1
,0
,1
,''''
,''''
,0
,0
,''''
,0
,0
,0
,0
,0
,0
,' + CONVERT(VARCHAR, @defaultDate) + '
,0
,''''
,''''
,0
,9
,0
,''PRIMARY''
,' + CONVERT(VARCHAR, @DEX_ROW_ID) + ' OUT'
SET @sql = '<?query -- ' + CHAR(13) + COALESCE(@sql,'') + + CHAR(13) + ' --?>'
SELECT CONVERT(XML, @sql)
--EXEC (@sql)
SELECT @O_ErrorCode = @@ERROR
GO
DECLARE @index INT ,
@row INT ,
@error INT
SET @index = 1
EXEC [dbo].[DGP_addVendor] 'LFD', @index, 'ANT0000001',
'Anthony Quisenberry', @row OUT, @error OUT, '8506 west Rd', '', '',
'Louisville', 'KY', '40247', 'USA', ''
SELECT @row AS [@row], @error AS [@error]
答案 1 :(得分:0)
您可以创建own system procedure。在master数据库中创建它,使用sp_前缀命名并标记为system one。结果,您可以从任何数据库中调用它,SP代码将访问调用数据库的对象。有可能但不推荐实现业务逻辑的方法。
另一种方法是使用动态SQL,正如您尝试的那样。这是一种更好的方式(从我的角度来看),但它自己有pros and cons。