简体   繁体   English

创建具有未知数据类型的包装器

[英]Create wrapper with unknown data types

I'm trying to create a wrapper in T-SQL for a procedure where I'm not sure what the data types are. 我正在尝试在T-SQL中为一个我不确定数据类型是什么的过程创建一个包装器。 I can run the wrapper without an INSERT INTO statement and I get the data just fine, but I need to have it in a table. 我可以在不使用INSERT INTO语句的情况下运行包装器,并且可以很好地获取数据,但是我需要将其存储在表中。

Whenever I use the INSERT INTO I get an error: 每当我使用INSERT INTO ,都会出现错误:

Column name or number of supplied values does not match table definition 列名或提供的值数与表定义不匹配

I've parsed back through my code and can't see where any column names don't match up, so I'm thinking that it has to be a data type. 我已经解析了我的代码,看不到任何列名不匹配的地方,因此我认为它必须是数据类型。 I've looked through the procedure I'm wrapping to see if I can find what the data types are, but some aren't defined there; 我浏览了我包装的过程,看是否可以找到数据类型,但是其中没有定义。 I've referenced the tables they pull some data from to find the definitions; 我引用了它们从中提取一些数据的表来查找定义。 I've run SQL_VARIANT_PROPERTY on all of the data to see what data type it is (although some of them come up null). 我已经在所有数据上运行SQL_VARIANT_PROPERTY以查看它是什么数据类型(尽管其中一些数据为null)。

Is there some better way for me to track down exactly where the error is? 有什么更好的方法可以让我准确地找出错误所在?

I think you can find out your stored procedure result schema, using sp_describe_first_result_set (available from SQL2012) and FMTONLY . 我认为您可以使用sp_describe_first_result_set (可从SQL2012获得)和FMTONLY来查找存储过程结果模式。 Something like this: 像这样:

EXEC sp_describe_first_result_set
    @tsql = N'SET FMTONLY OFF; EXEC yourProcedure <params are embedded here>'

More details can be found here . 可以在此处找到更多详细信息。

However, if I remember correctly, this works only if your procedure used deterministic schemas (no SELECT INTO #tempTable or similar things). 但是,如果我没记错的话,这仅在您的过程使用确定性模式(没有SELECT INTO #tempTable或类似内容)时才有效。

One trick to find out the schema of your result is to actually materialize the result into ad-hoc created table. 找出结果模式的一个技巧是将结果实际具体化到临时创建的表中。 However, this is not easy since SELECT INTO does not work with EXEC procedure . 但是,这并不容易,因为SELECT INTO不适用于EXEC procedure One work-around is this: 一种解决方法是:

1) Define a linked-server to the instance itself. 1)定义一个到实例本身的链接服务器。 Eg loopback 例如环回

2) Execute your procedure like this (for SQL 2008R2 ): 2)执行以下过程(对于SQL 2008R2 ):

    SELECT * INTO tempTableToHoldDataAndStructure
    FROM OPENQUERY(' + @LoopBackServerName + ', ''set fmtonly off exec ' + @ProcedureFullName + ' ' + @ParamsStr

where 哪里

@LoopBackServerName = 'loopback'
@ProcedureFullName = loopback.database.schema.procedure_name
@ParamsStr = embedded parameters

For SQL2012 I think the execution might fail if RESULT SETS are not provided (ie schema definition of the expected result, which is kind of a chicken-egg problem in this case): 对于SQL2012我认为如果未提供RESULT SETS(即,预期结果的架构定义,在这种情况下是种鸡蛋问题),则执行可能会失败:

' WITH RESULT SETS (( ' +  @ResultSetStr + '))'');

Okay, I have a solution to my problem. 好的,我有解决问题的办法。 It's tedious, but tedious I can do. 这很乏味,但我可以做到。 Randomly guessing is what drives me crazy. 随机猜测是让我发疯的原因。 The procedure I'm wrapping dumps 51 columns. 我包装的过程将转储51列。 I already know I can get it to work without putting anything into a table. 我已经知道我可以在不放入任何东西的情况下使它正常工作。 So I decided to comment out part of the select statement in the procedure I'm wrapping so it's only selecting 1 column. 因此,我决定在我要包装的过程中注释掉部分select语句,因此仅选择1列。 (First I made a copy of that procedure so I don't screw up the original; then I referenced the copy from my wrapper). (首先,我制作了该程序的副本,以免弄乱了原件;然后从包装器中引用了该副本)。 Saved both, ran it, and it worked. 保存两个,运行它,它就起作用了。 So far so good. 到现在为止还挺好。 I could have done it line by line, but I'm more of a binary kind of guy, so I went about halfway down--now I'm including about 25 columns in both the select statement and my table--and it's still working. 我可以逐行完成它,但是我更像是一个二进制人,所以我走了一半左右—现在我在select语句和表中都包含了约25列–现在工作。 Repeat procedure until it doesn't work any more, then backtrack until it does again. 重复该过程,直到不再起作用,然后回溯,直到再次起作用。 My error was in identifying one of the data types followed by "IDENTITY". 我的错误是在识别“ IDENTITY”之后的一种数据类型。 I'm not sure what will happen when I leave that out, but at least my wrapper works. 我不确定将其忽略时会发生什么,但是至少我的包装起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ的.Union()具有未知数据类型 - LINQ's .Union() with unknown data types 选择不同的数据类型来创建视图 - selecting different data types to create a view 如何在SQL Server 2008中创建自定义数据类型? - How to create custom data types in SQL Server 2008? 用于为事务复制创建发布的包装器存储过程 - Wrapper stored procedure to create publication for transactional replication 是否可以从具有不同数据类型的键值表创建动态数据透视查询? - Can I create dynamic pivot query from key value table with different data types? 是否可以从现有的SQL Server中创建一个新表并更改列数据类型? - Is it possible to create a new table in SQL Server from an existing one and change the column data types? 禁止 CREATE TABLE FROM SELECT 时如何获取列的数据类型? - How to get data types of columns when a CREATE TABLE FROM SELECT is forbidden? 数据类型 varchar 和 varbinary(max) 在 add 运算符中不兼容。 在尝试创建存储过程时 - The data types varchar and varbinary(max) are incompatible in the add operator. while trying to create stored procedure 使用某些数据类型转换在链接的SQL Server 2005数据库中创建视图 - To create a view in a linked SQL Server 2005 database with some data types transformations 如何使用 SQL 中的动态数据屏蔽创建具有不同屏蔽类型的多个用户? - How can I create multiple users with different masking types using Dynamic Data Masking in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM