Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
hii..

my problem in sql stores procedure

i want to pass @vquery1 in insert command.. @vquery1 is varchar(max),

SQL
USE [SCJ]
GO
/****** Object:  StoredProcedure [dbo].[spGetCHLDETAIL1]    Script Date: 03/20/2014 01:46:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<author,,name>
-- Create date: <create>
-- Description:	<description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spGetCHLDETAIL1] 
	-- Add the parameters for the stored procedure here
	 @DistrictID VARCHAR(250),
     @barcode 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), 
    @vquery1 AS varchar(max),
     @vQuery AS VARCHAR (max);
   
     
    SET @ChildTblName = '[' + @DistrictID + 'Child_D' + ']'
    SET @FamilyTblName = '[' + @DistrictID + 'Family_D' + ']'

    -- Insert statements for procedure here
	   SET @vQuery = 'SELECT Child.scanno,Child.cname,Child.distt,Child.block,Child.gpmctc,Child.village ,Child.habitation, Child.barcode,Child.sex,Child.dob,Family.pincode,Family.hhno,Family.fname,Family.mname,Family.gname,Family.social_grp,Family.minority,Family.total_male,Family.total_fema,Family.seasonal_m,Family.migration_,Family.duration,Family.lp,Family.up,Family.sec,Family.catch_s_lp,Family.catch_s_up,Family.catch_s_se,Child.location,
    Child.residing,Child.mother_ton,Child.disability,Child.sch_code,Child.class,Child.nereason,Child.droupout,Child.highclass
    FROM ' + @ChildTblName + ' As Child  WITH (NOLOCK) INNER JOIN ' + @FamilyTblName + ' AS Family WITH (NOLOCK) ON 
    Child.barcode=Family.barcode WHERE LTRIM(RTRIM(Child.cname)) <> '''' AND Child.distt = ' + @DistrictID + '';
   
    -- EXEC(@vQuery)
 
   -- exec(@vquery1)
  insert into CHLDINFO_LOG(Chld_Name) values(@vquery)

i will pass more parameter.. i m check for one value..

it's not working ..

please help.. it's urgent..


thanks in advance
Posted
Updated 20-Mar-14 1:42am
v4
Comments
Richard MacCutchan 20-Mar-14 7:00am    
it's not working
And you expect us to guess what that means?

it's urgent.
no it's not.
King Fisher 20-Mar-14 7:21am    
SET @vquery1='select Child.cname FROM ' + @ChildTblName + ' As Child INNER JOIN ' + @FamilyTblName + ' AS Family ON
Child.barcode=Family.barcode WHERE Child.distt = ' + @DistrictID + '';


you must pass value for those vaiables.bad one ;)

SQL
--How to create the procedure
CREATE PROCEDURE [dbo].[InsertMyValue] 
@vquery1 VARCHAR(MAX)
AS 
BEGIN
insert into CHLDINFO_LOG(Chld_Name) values(@vquery1 )
END

--To execute the procedure
EXEC InsertMyValue 'ABC'
 
Share this answer
 
v3
Comments
bindash 20-Mar-14 7:11am    
it's not working .. my query is

SET @vquery1='select Child.cname FROM ' + @ChildTblName + ' As Child INNER JOIN ' + @FamilyTblName + ' AS Family ON
Child.barcode=Family.barcode WHERE Child.distt = ' + @DistrictID + '';
Sibasisjena 20-Mar-14 7:14am    
What do you want? you want to create a procedure or what ?
And what is the problem you are facing.
bindash 20-Mar-14 7:24am    
my procedure is for display data in labels in web page..it's working.. the procedure has more field ex..child name,disstt,clock etc of 2 tables.. i want to store this data in a table childinfo_log
Sibasisjena 20-Mar-14 7:31am    
you want a procedure which will insert multiple values into the table.
Am i correct ?
bindash 20-Mar-14 7:34am    
yes my procedure is working for diasplay data.. i want to store this display data in a table
hi. Try this. I think you left single inverted comma (') in where clause

USE [SCJ]
GO
/****** Object:  StoredProcedure [dbo].[spGetCHLDETAIL1]    Script Date: 03/20/2014 01:46:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<author,,name>
-- Create date: <create>
-- Description:	<description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spGetCHLDETAIL1] 
	-- Add the parameters for the stored procedure here
	 @DistrictID VARCHAR(250),
     @barcode 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), 
    @vquery1 AS varchar(max),
     @vQuery AS VARCHAR (max);
   
     
    SET @ChildTblName = '[' + @DistrictID + 'Child_D' + ']'
    SET @FamilyTblName = '[' + @DistrictID + 'Family_D' + ']'
 
    -- Insert statements for procedure here
	   SET @vQuery = 'SELECT Child.scanno,Child.cname,Child.distt,Child.block,Child.gpmctc,Child.village ,Child.habitation, Child.barcode,Child.sex,Child.dob,Family.pincode,Family.hhno,Family.fname,Family.mname,Family.gname,Family.social_grp,Family.minority,Family.total_male,Family.total_fema,Family.seasonal_m,Family.migration_,Family.duration,Family.lp,Family.up,Family.sec,Family.catch_s_lp,Family.catch_s_up,Family.catch_s_se,Child.location,
    Child.residing,Child.mother_ton,Child.disability,Child.sch_code,Child.class,Child.nereason,Child.droupout,Child.highclass
    FROM ' + @ChildTblName + ' As Child  WITH (NOLOCK) INNER JOIN ' + @FamilyTblName + ' AS Family WITH (NOLOCK) ON 
    Child.barcode=Family.barcode WHERE LTRIM(RTRIM(Child.cname)) <> '''' AND Child.distt = ''' + @DistrictID + '''';
   
    -- EXEC(@vQuery)
 


you can use print to check the statement
--print @vQuery

EXEC(@vQuery)


   -- exec(@vquery1)
  --insert into CHLDINFO_LOG(Chld_Name) values(@vquery)
 
Share this answer
 
Don't call exec(@vquery1). Just call
SQL
insert into CHLDINFO_LOG(Chld_Name) values(@vquery1 )
 
Share this answer
 
Comments
bindash 20-Mar-14 7:10am    
it's not working...
my query is

SET @vquery1='select Child.cname FROM ' + @ChildTblName + ' As Child INNER JOIN ' + @FamilyTblName + ' AS Family ON
Child.barcode=Family.barcode WHERE Child.distt = ' + @DistrictID + '';

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