Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,
i am new in development.. please give me idea about syntax..
my sp is

Child.cname='+@CHILDNAME+' in this error is occures

the error is: invalid column namewhen sp run and put value of disttcid and childname


SQL
USE [SCJ]
GO
/****** Object:  StoredProcedure [dbo].[spGetCHLDETAIL1]    Script Date: 03/11/2014 12:43:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:	<author,,name>
-- Create date: <create>
-- Description:	<description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spMODIFY] 
	-- Add the parameters for the stored procedure here
	 @DistrictID VARCHAR(250),
    
     @CHILDNAME VARCHAR(50)  
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE
	@ChildTblName AS VARCHAR (125), 
    @FamilyTblName AS VARCHAR (125), 
   
     @vQuery AS VARCHAR (max);
   
     
    SET @ChildTblName = '[' + @DistrictID + 'Child_D' + ']'
    SET @FamilyTblName = '[' + @DistrictID + 'Family_D' + ']'

    -- Insert statements for procedure here
	   SET @vQuery = 'UPDATE Child SET Child.cname='+@CHILDNAME+' 
    FROM ' + @ChildTblName + ' As Child'; 
  
   

 EXEC(@vQuery)
 
END

please help me..
thanks in advance
Posted
Updated 12-Mar-14 0:48am
v2
Comments
Dinesh.V.Kumar 12-Mar-14 7:34am    
Print the @vQuery and run it separately and check...I suppose the update statement as a whole is wrong..

Regards,

1 solution

Before EXEC(@vQuery) put a line PRINT @vQuery and examine it.
Assuming you have set @CHILDNAME to 'AName', the line of code...
SQL
SET @vQuery = 'UPDATE Child SET Child.cname='+@CHILDNAME+'
    FROM ' + @ChildTblName + ' As Child';

will result in the PRINT
UPDATE Child SET Child.cname=AName FROM tablename As Child

You won't have a column on your table called AName - hence the error. @CHILDNAME (I guess) is expected to contain a string of characters, in which case you will need to surround it with single quotes when you are constructing the SQL query to EXECute. Try this instead
SQL
SET @vQuery = 'UPDATE Child SET Child.cname=' + CHAR(39) +@CHILDNAME+ CHAR(39) + ' 
    FROM ' + @ChildTblName + ' As Child';

You could "escape" the required single quote characters (example[^]) but I prefer to explcitly use CHAR(39) to make the code clearer
 
Share this answer
 
Comments
Corporal Agarn 12-Mar-14 9:50am    
If after examining the output you cannot find a problem, try running the output from the PRINT on a test system.
bindash 13-Mar-14 1:07am    
THANKS A LOT.. u r genius..
Maciej Los 12-Mar-14 18:02pm    
Good work! +5!

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