Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
SQL
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 tried this to put the parameter in calling stored procedure as SqlDbType.NVarChar but it doesnt work too

please any solution?
Posted
Updated 6-Jan-15 1:39am
v2
Comments
Tomas Takac 6-Jan-15 7:45am    
The error comes from sp_rename.
Mike Meinz 6-Jan-15 7:50am    
Maybe there is no column name with the name @ColumnName + '_Spare'
oula alsheikh 6-Jan-15 8:22am    
in first statement i alter the table and add a acolumn having the word spare so it exists

1 solution

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

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...
 
Share this answer
 
v2
Comments
oula alsheikh 6-Jan-15 8:19am    
i mentioned that there is no error when i call the procedure in Sql Server Management Studio the problem is not from sp_rename or i think it is not from procedure but from calling the procedure
Kornfeld Eliyahu Peter 6-Jan-15 8:24am    
Of course there is - it just do not pop into your face...Read your code and you can see that the order is wrong, you try to rename a column that does not exists at that point!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900