参数@objname是不明确的,或者声明的@objtype(列)是错误的。 [英] Either the parameter @objname is ambiguous or the claimed @objtype (Column) is wrong.

查看:409
本文介绍了参数@objname是不明确的,或者声明的@objtype(列)是错误的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下存储过程,根据我的情况做了很多任务

我的问题不在存储过程中,因为它在sql中工作正常并做我想要的事情

但我的问题是在C#.net(Ado.net技术)中调用这个存储过程

因为当我调用该过程时我得到以下异常

参数@objname不明确或声称@objtype(列)错误。

调用存储过程代码是

i have the following stored procedure that makes many tasks according to my situation
my problem is not in the stored procedure because it works fine in sql and does what i want
but my problem is in calling this stored procedure in C#.net (Ado.net technique)
because when i call the procedure i got the following exception
Either the parameter @objname is ambiguous or the claimed @objtype (Column) is wrong.
calling stored procedure code is

public void CreatePrimaryKeyOnly(string PT,string PK)
        {

            using (SqlConnection connection = new SqlConnection(connection_string))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("CreatePrimaryKeyOnly", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@TableName", SqlDbType.VarChar).Value = PT;
                command.Parameters.Add("@ColumnName", SqlDbType.VarChar).Value = PK;
                command.ExecuteNonQuery();
                connection.Close();
            }

        }



和存储过程是


and the stored procedure is

ALTER Procedure [dbo].[CreatePrimaryKeyOnly]
@TableName nvarchar(255),
@ColumnName nvarchar(255)
as
--Alter Table Sys_Tables Add TableID_Spare  uniqueidentifier Not null Default(newid())
Declare @AddColumn nvarchar(2000)
Set @AddColumn='Alter Table [' + @TableName + '] Add ' + @ColumnName + '_Spare  uniqueidentifier Not null Default(newid()) '
Exec(@Addcolumn)

--delete Basic primary key 
Declare @GetPrimayKeyName nvarchar(2000)
SELECT @GetPrimayKeyName = 'ALTER TABLE [' + @TableName
   + '] DROP CONSTRAINT ' + name + ';'
    FROM sys.key_constraints
    WHERE [type] = 'PK'
    AND [parent_object_id] = OBJECT_ID(@TableName);
	EXEC sp_executeSQL @GetPrimayKeyName

--Sp_rename '[Sys_Tables].TableID','forJoin','Column'
Declare @JoinRenameStatement nvarchar(1000)
Set @JoinRenameStatement='Sp_rename ''['+@TableName+'].'+@ColumnName + ''' , ForJoin,''Column'''
Exec(@JoinRenameStatement )


declare @strsql varchar(2000)
--Primary Key Rename
--Example sp_rename '[Sys_Tables].TableID_Spare' , 'TableID', 'COLUMN'
set @strsql = 'Sp_rename ''['+@TableName+'].'+@ColumnName+ +'_Spare'+ ''' , '''+@ColumnName+''',''Column''' 
exec (@strsql)


declare @PrimaryKeyStatement varchar(2000)
--Alter Table Outcomes Add Constraint OutcomesIDPK Primary Key(OutcomeID)
Set @PrimaryKeyStatement= 'Alter Table ['+ @TableName + '] Add Constraint ' + @ColumnName +  'PK Primary Key(' + @ColumnName + ')'
Exec(@PrimaryKeyStatement)



i尝试将此参数放入调用存储中程序如SqlDbType.NVarChar但它也不工作



请问任何解决方案?


i tried this to put the parameter in calling stored procedure as SqlDbType.NVarChar but it doesnt work too

please any solution?

推荐答案

Declare @JoinRenameStatement nvarchar(1000)
Set @JoinRenameStatement='Sp_rename ''['+@TableName+'].'+@ColumnName + ''' , ForJoin,''Column'''
Exec(@JoinRenameStatement )



此处指出没有这样的列@ TableName。@ ColumnName ...你最初用s后缀'_Spare'创建它,部件会在你之前重命名它...


At this point no such column @TableName.@ColumnName...You originally created it with s suffix '_Spare' and the part will rename it back still before you...


这篇关于参数@objname是不明确的,或者声明的@objtype(列)是错误的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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