Related
My database developer gave me a SQL query and it runs successfully in SSMS.
USE testdb
DECLARE #Pipeline TABLE
(
pipeline VARCHAR(4000),
unitcode VARCHAR(4000),
designareanumber VARCHAR(4000),
nameofnote VARCHAR(4000)
)
INSERT INTO #Pipeline
SELECT pipeline,
unitcode,
designareanumber,
nameofnote
FROM (SELECT pp.oid AS OID,
nmpp.itemname AS ItemName,
nmpl.itemname AS Pipeline,
tn.trainnumber AS TrainNumber,
uc.unitcode AS UnitCode,
FROM jrtepipepart pp
JOIN jnameditem nmpp
ON pp.oid = nmpp.oid
JOIN xcontainsnote xcn
ON pp.oid = xcn.oidorigin
WHERE gn.text <> ''
AND ( nmpps.itemname = 'Piping_New'
OR nmpps.itemname = 'Piping_Modified/Tie_In' )
--where gn.Text like '%000002A_TP08' AND (nmpps.ItemName = 'Piping_New' OR nmpps.ItemName = 'Piping_Modified/Tie_In')
UNION ALL
SELECT hs.oid AS OID,
nmhs.itemname AS ItemName,
nmpl.itemname AS Pipeline,
tn.trainnumber AS TrainNumber,
uc.unitcode AS UnitCode,
FROM jhgrsupport hs
JOIN jnameditem nmhs
ON hs.oid = nmhs.oid
WHERE gn.text <> ''
AND ( nmpps.itemname = 'Piping_New'
OR nmpps.itemname = 'Piping_Modified/Tie_In' ))A
SELECT pipeline,
unitcode,
designareanumber,
( Stuff((SELECT Cast('; ' + nameofnote AS VARCHAR(max))
FROM #Pipeline p2
WHERE ( p2.pipeline = p1.pipeline
AND p2.unitcode = p1.unitcode
AND p2.designareanumber = p1.designareanumber
AND nameofnote NOT IN ( 'Note 1', '' ) )
FOR xml path ('')), 1, 2, '') ) AS NameOfNote
FROM #Pipeline p1
WHERE nameofnote NOT IN ( 'Note 1', '' )
GROUP BY pipeline,
unitcode,
designareanumber
When I run above query in OleDbDataAdapter it return 0 rows. I have tried adding SET NOCOUNT ON at start of the query but not working. I have increased set-timeout property.
Removed USE testdb, added SET NOCOUNT ON and added database name before table names resolved this issue.
I have a DataSet like below :
Where B1,B2 C1,C2 and C3 are the Column names. G1,G2,S1 and T1 are the first Row elements of my Data Set.
Now i want to combine Similar Columns/ROws in to Group .
Example : Columns B1, B2 and B3 combines to a single group B,
Rows : G1 and G2 Combines together to form a single row G.
Below is the O/P DataSet i need.
I have tried to use Dictionaries and DataSet Loops using but cant get this O/P .
Can anyone help me out with this.
This having to be dynamic adds a huge amount of complexity to this solution. As you haven't responded to the version question, I have not used STRING_AGG, however, if you are using SQL Server 2017+ you can simplify the query to use it.
Firstly, some sample data:
CREATE TABLE dbo.Matrix ([Data] char(2),
B1 tinyint,
B2 tinyint,
C1 tinyint,
C2 tinyint,
C3 tinyint)
INSERT INTO dbo.Matrix ([Data],
B1,
B2,
C1,
C2,
C3)
VALUES('G1',1,1,2,2,4),
('G2',1,1,1,1,1),
('S1',2,1,2,1,1),
('T1',1,3,2,2,3);
GO
Now, if this wasn't dynamic, you could use a a Cross tab to pivot the data into groups, like this:
SELECT LEFT(M.[Data],1) AS [Data],
SUM(CASE V.Col WHEN 'B' THEN V.ColVal END) AS B,
SUM(CASE V.Col WHEN 'C' THEN V.ColVal END) AS C
FROM dbo.Matrix M
CROSS APPLY(VALUES('B',M.B1),
('B',M.B2),
('C',M.C1),
('C',M.C2),
('C',M.C3))V(Col,ColVal)
GROUP BY LEFT(M.[Data],1);
Unfortunately, as it is dynamic then we need dynamic SQL. Honestly, this isn't beginning stuff, and I'm not here to support this SQL; it's up to you to understand it, maintain it, support it, and (because it is dynamic SQL) keep it secure. I'm happy to answer some questions on how it works, but for someone that doesn't knowe SQL well this is a steep learning curve:
DECLARE #SQL nvarchar(MAX);
SET #SQL = N'SELECT LEFT(M.[Data],1) AS [Data],' + NCHAR(13) + NCHAR(10) +
STUFF((SELECT N',' + NCHAR(13) + NCHAR(10) +
N' SUM(CASE V.Col WHEN N' + QUOTENAME(LEFT(C.COLUMN_NAME,1),'''') + N' THEN V.ColVal END) AS ' + QUOTENAME(LEFT(C.COLUMN_NAME,1))
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.TABLE_SCHEMA = N'dbo'
AND C.TABLE_NAME = N'Matrix'
AND C.COLUMN_NAME != N'Data' --Assumes that all other columns are applicable
GROUP BY LEFT(C.COLUMN_NAME,1)
ORDER BY LEFT(C.COLUMN_NAME,1)
FOR XML PATH(N''),TYPE).value('.','nvarchar(MAX)'),1,3,N'') + NCHAR(13) + NCHAR(10) +
N'FROM dbo.Matrix M' + NCHAR(13) + NCHAR(10) +
N' CROSS APPLY(VALUES' + STUFF((SELECT ',' + NCHAR(13) + NCHAR(10) +
N' (N' + QUOTENAME(LEFT(C.COLUMN_NAME,1),'''') + N',M.' + QUOTENAME(C.COLUMN_NAME) + N')'
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.TABLE_SCHEMA = N'dbo'
AND C.TABLE_NAME = N'Matrix'
AND C.COLUMN_NAME != N'Data' --Assumes that all other columns are applicable
ORDER BY C.COLUMN_NAME
FOR XML PATH(N''),TYPE).value('.','nvarchar(MAX)'),1,26,N'') + N')V(Col,ColVal)' + NCHAR(13) + NCHAR(10) +
N'GROUP BY LEFT(M.[Data],1)' + NCHAR(13) + NCHAR(10) +
N'ORDER BY LEFT(M.[Data],1);';
PRINT #SQL; --Your debugging best friend.
EXEC sp_executesql #SQL;
db<>fiddle
You can apply below logic to get desired output from SQL Server:
create table T_DataSet
(
Data varchar(50),
B1 int,
B2 int,
C1 int,
C2 int,
C3 int
)
INSERT INTO T_DataSet ([Data],B1,B2,C1,C2,C3)
VALUES('G1',1,1,2,2,4),
('G2',1,1,1,1,1),
('S1',2,1,2,1,1),
('T1',1,3,2,2,3);
DECLARE #QUERY VARCHAR(MAX)=''
DECLARE #Columns VARCHAR(MAX)=''
;with tbl_COLUMN_NAME (COLUMN_NAME) AS
(
select name as COLUMN_NAME from sys.all_columns
where object_id = (select object_id from sys.tables where name = 'T_DataSet')
and name <> 'Data'
)
SELECT
#Columns = ISNULL(#Columns +',', '') + T.COLUMN_NAME
FROM
(
select
COLUMN_NAME = 'SUM(' +
(select SUBSTRING(
(
SELECT '+'+ COLUMN_NAME
FROM tbl_COLUMN_NAME
where LEFT(COLUMN_NAME,1) = LEFT(inner_C1.COLUMN_NAME,1)
FOR XML PATH('')
), 2 , 9999))
+ ') AS ' + LEFT(COLUMN_NAME,1)
from tbl_COLUMN_NAME as inner_C1
Group by LEFT(COLUMN_NAME,1)
)T
set #QUERY = 'select LEFT([Data],1) as Data ' + #Columns + '
From T_DataSet
Group by LEFT([Data],1)';
PRINT #QUERY
EXEC(#QUERY)
I have stored procedure, which works fine if executed by SQL Server management studio. When Calling it from ASP.net MVC app it throws temporary table exception like Invalid object name '##temp_demographic_data_set'.
Any Clue, Why this error occurs when execute from C#.
Below is the stored procedure Code:
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_get_demographic_history_csv]
#_startdate datetime,
#_enddate datetime,
#_nccs varchar(56),
#_megacity varchar(56),
#_10lac varchar(56),
#_below10 varchar(56),
#_rural varchar(56),
#_state_megacity varchar(56) =NULL,
#_state_10lac varchar(56) =NULL,
#_state_below10 varchar(56) =NULL,
#_state_rural varchar(56) =NULL,
#_gender varchar(56),
#_agegroup varchar(56),
#limt int =NULL,
#off_set int =NULL,
#_Subscription_start datetime,
#_Subscription_end datetime,
#_demographic_field_id varchar(max),
#templateid int
AS
BEGIN
DECLARE #demographic_columns varchar(max);
DECLARE #temp_query nvarchar(max);
DECLARE #parameter_defination_list nvarchar(max);
DECLARE #parameter_list nvarchar(max);
-- Variables to check whether user is allowed to query on columns or not
DECLARE #is_nccs_exist varchar(20);
DECLARE #is_agegroup_exist varchar(20);
DECLARE #is_stategroup_exist int;
DECLARE #is_townclass_exist int;
DECLARE #is_gender_exist varchar(20);
DECLARE #demographic_view nvarchar(max);
DECLARE #demographic_report varchar(max);
DECLARE #record_count int;
-- DECLARE #tableid varchar(56);
SET XACT_ABORT ON
SET NOCOUNT ON
-- Set Defaults
SET #is_nccs_exist ='false';
SET #is_agegroup_exist ='false';
SET #is_stategroup_exist =0;
SET #is_townclass_exist =0;
SET #is_gender_exist ='false';
-- Get values from the template
SELECT
#is_gender_exist=max(CASE WHEN demofield.name='master_gender' THEN 'true' ELSE 'false' END),
#is_townclass_exist=max(CASE WHEN demofield.name='master_town_class' THEN 1 ELSE 0 END) ,
#is_stategroup_exist=max(CASE WHEN demofield.name='master_state_group' THEN 1 ELSE 0 END),
#is_agegroup_exist=max(CASE WHEN demofield.name='master_age_group' THEN 'true' ELSE 'false' END),
#is_nccs_exist=max(CASE WHEN demofield.name='master_nccs' THEN 'true' ELSE 'false' END)
FROM dbo.template_demographic_fields template
INNER JOIN demgraphic_fields demofield on template.field_id=demofield.id
WHERE demofield.is_inventory_field=1 and (#templateid=0 OR template_id=#templateid);
-- Update Variables
SET #is_nccs_exist =COALESCE(#is_nccs_exist,'false');
SET #is_agegroup_exist =COALESCE(#is_agegroup_exist,'false');
SET #is_stategroup_exist=COALESCE(#is_stategroup_exist,0);
SET #is_townclass_exist =COALESCE(#is_townclass_exist,0);
SET #is_gender_exist =COALESCE(#is_gender_exist,'false');
exec dbo.sp_split_string_into_rows #_demographic_field_id;
-- Get Demographic columns from table
select #demographic_columns=
(
COALESCE(#demographic_columns+ ',', '') +
CAST( (case when return_as is not null then name+' as '+ '['+return_as+']' else '['+name+']' end) AS VARCHAR(max))
)
from dbo.demgraphic_fields
where is_inventory_field=0 and ( #templateid=0 OR id in (select cast(split_data as int) from ##temp_convert));
set #demographic_columns=#demographic_columns+','+'state_group as state_group_code,town_class as town_class_code,
nccs_group as nccs_group_code,age_group as age_group_code, sex as gender,file_date,member_id';
select #demographic_report=
(
COALESCE(#demographic_report+ ',', '') +
CAST( (case when return_as is not null then '['+return_as+']' else '['+name+']' end) AS VARCHAR(max))
)
from dbo.demgraphic_fields
where is_inventory_field=0 and ( #templateid=0 OR id in (select cast(split_data as int) from ##temp_convert));
select #demographic_view=
(
COALESCE(#demographic_view+ ',', '') +
CAST( (case when return_as is not null then '['+return_as+'] VARCHAR(56),' else '['+name+'] VARCHAR(56),' end) AS VARCHAR(max))
)
from dbo.demgraphic_fields
where is_inventory_field=0 and ( #templateid=0 OR id in (select cast(split_data as int) from ##temp_convert));
SET #demographic_view='CREATE TABLE ##temp_demographic_data_set ('+(#demographic_view+'state_group_code VARCHAR(56), town_class_code VARCHAR(56),
nccs_group_code VARCHAR(56),age_group_code VARCHAR(56), gender VARCHAR(56),file_date datetime,member_id VARCHAR(56)')+');'
if object_id('tempdb..##temp_demographic_data_set') is not null drop table ##temp_demographic_data_set ;
execute sp_executesql #demographic_view;
-- drop temporary table if already exist & create temporary table
if object_id('tempdb..#temp_state_megacity') is not null drop table #temp_state_megacity ;
-- execute procedure to split comman seprated input value into rows
exec dbo.sp_split_string_into_rows #_state_megacity;
-- get comma separated to row converted value into temporary table.
select distinct _stategroup_code.*,_stategroup.is_exposed into #temp_state_megacity from _stategroup_code
INNER JOIN _stategroup on _stategroup_code.stategroup_id=_stategroup.id
where #templateid=0 OR
_stategroup_code.stategroup_id in
(
select split_data from ##temp_convert
WHERE split_data in (SELECT master_id from master_template where master_type='STATE_GROUP' and (template_id=#templateid))
);
if object_id('tempdb..#temp_state_10lac') is not null drop table #temp_state_10lac ;
exec dbo.sp_split_string_into_rows #_state_10lac;
select distinct _stategroup_code.*,_stategroup.is_exposed into #temp_state_10lac from _stategroup_code
INNER JOIN _stategroup on _stategroup_code.stategroup_id=_stategroup.id
where #templateid=0 OR _stategroup_code.stategroup_id in
(
select split_data from ##temp_convert
WHERE split_data in (SELECT master_id from master_template where master_type='STATE_GROUP' and (template_id=#templateid))
);
if object_id('tempdb..#temp_state_below10') is not null drop table #temp_state_below10 ;
exec dbo.sp_split_string_into_rows #_state_below10;
select distinct _stategroup_code.*,_stategroup.is_exposed into #temp_state_below10 from _stategroup_code
INNER JOIN _stategroup on _stategroup_code.stategroup_id=_stategroup.id
where #templateid=0 OR _stategroup_code.stategroup_id in
(
select split_data from ##temp_convert
WHERE split_data in (SELECT master_id from master_template where master_type='STATE_GROUP' and (template_id=#templateid))
);
if object_id('tempdb..#temp_state_rural') is not null drop table #temp_state_rural ;
exec dbo.sp_split_string_into_rows #_state_rural;
select distinct _stategroup_code.*,_stategroup.is_exposed into #temp_state_rural
from _stategroup_code
INNER JOIN _stategroup on _stategroup_code.stategroup_id=_stategroup.id
where #templateid=0 OR _stategroup_code.stategroup_id in
(
select split_data from ##temp_convert
WHERE split_data in (SELECT master_id from master_template where master_type='STATE_GROUP' and (template_id=#templateid))
);
if object_id('tempdb..#temp_nccs') is not null drop table #temp_nccs;
exec dbo.sp_split_string_into_rows #_nccs;
select distinct * into #temp_nccs from _nccs_code
where #templateid=0 OR _nccs_code.nccs_id in
(
select split_data from ##temp_convert
WHERE split_data in (SELECT master_id from master_template where master_type='NCCS' and (template_id=#templateid))
);
if object_id('tempdb..#temp_gender') is not null drop table #temp_gender ;
exec dbo.sp_split_string_into_rows #_gender;
select distinct * into #temp_gender from ##temp_convert;
if object_id('tempdb..#temp_agegroup') is not null drop table #temp_agegroup ;
exec dbo.sp_split_string_into_rows #_agegroup;
select distinct * into #temp_agegroup from _agegroup_code
where #templateid=0 OR _agegroup_code.agegroup_id in
(
select split_data from ##temp_convert
WHERE split_data in (SELECT master_id from master_template where master_type='AGE_GROUP' and template_id =#templateid)
);
if object_id('tempdb..#temp_demographic_data') is not null drop table #temp_demographic_data ;
SET #parameter_defination_list= ' #temp_startdate datetime,
#temp_enddate datetime,
#temp_Subscription_start datetime,
#temp_Subscription_end datetime,
#temp_is_nccs_exist varchar(56),
#temp_is_agegroup_exist varchar(56),
#temp_is_gender_exist varchar(56)';
-- Loading Data To Temporary table for the filters provided
SET #temp_query=
'INSERT INTO ##temp_demographic_data_set SELECT '+#demographic_columns+'
FROM dbo.raw_demographic_history
where (file_date between cast(#temp_Subscription_start as datetime) and cast(#temp_Subscription_end as datetime)) and
(file_date between cast(#temp_startdate as datetime) and cast(#temp_enddate as datetime) )
and
(NOT #temp_is_nccs_exist=''true'' OR nccs_group in (select code from #temp_nccs) )and
(NOT #temp_is_gender_exist=''true'' OR sex in (select * from #temp_gender) )and
(NOT #temp_is_agegroup_exist=''true'' OR age_group in (select code from #temp_agegroup) );';
execute sp_executesql #temp_query ,#parameter_defination_list,
#temp_startdate=#_startdate ,
#temp_enddate=#_enddate,
#temp_Subscription_start=#_Subscription_start ,
#temp_Subscription_end=#_Subscription_end,
#temp_is_nccs_exist=#is_nccs_exist,
#temp_is_agegroup_exist=#is_agegroup_exist,
#temp_is_gender_exist=is_gender_exist;
-- Creating table with empty columns
SELECT * INTO #temp_demographic_data FROM ##temp_demographic_data_set WHERE state_group_code=0;
-- Create Indexes
CREATE INDEX IX_TEST_temp_demographic_data_1 ON #temp_demographic_data(nccs_group_code);
CREATE INDEX IX_TEST_temp_demographic_data_2 ON #temp_demographic_data(town_class_code);
CREATE INDEX IX_TEST_temp_demographic_data_3 ON #temp_demographic_data(age_group_code);
CREATE INDEX IX_TEST_temp_demographic_data_4 ON #temp_demographic_data(state_group_code);
CREATE INDEX IX_TEST_temp_demographic_data_5 ON #temp_demographic_data(member_id);
CREATE INDEX IX_TEST_file_date_6 ON #temp_demographic_data(file_date);
-- When All India IS True
IF (#_state_megacity is null AND #_state_10lac is null AND #_state_below10 is null AND #_state_rural is null)
BEGIN
INSERT INTO #temp_demographic_data SELECT * FROM ##temp_demographic_data_set;
-- SET Town class to empty
/*
UPDATE #temp_demographic_data
SET town_class_code=NULL
WHERE (state_group_code in (select code from #temp_state_megacity WHERE is_exposed=0)) OR
(state_group_code in (select code from #temp_state_10lac WHERE is_exposed=0)) OR
(state_group_code in (select code from #temp_state_below10 WHERE is_exposed=0)) OR
(state_group_code in (select code from #temp_state_rural WHERE is_exposed=0)) ;
*/
END
ELSE
BEGIN
-- WHEN All India is false and state,town columns exist in template
IF #is_stategroup_exist=1 and #is_townclass_exist=1
BEGIN
-- Update Town class to empty for states not exposed to user
INSERT INTO #temp_demographic_data
SELECT * FROM
(
SELECT * FROM ##temp_demographic_data_set
WHERE
(state_group_code in (select code from #temp_state_megacity WHERE is_exposed=0)) OR
(state_group_code in (select code from #temp_state_10lac WHERE is_exposed=0)) OR
(state_group_code in (select code from #temp_state_below10 WHERE is_exposed=0)) OR
(state_group_code in (select code from #temp_state_rural WHERE is_exposed=0))
UNION ALL
SELECT * FROM ##temp_demographic_data_set
WHERE
(town_class_code=#_megacity and state_group_code in (select code from #temp_state_megacity WHERE is_exposed=1)) OR
(town_class_code=#_10lac and state_group_code in (select code from #temp_state_10lac WHERE is_exposed=1)) OR
(town_class_code=#_below10 and state_group_code in (select code from #temp_state_below10 WHERE is_exposed=1)) OR
(town_class_code=#_rural and state_group_code in (select code from #temp_state_rural WHERE is_exposed=1))
)demographic_data;
/*UPDATE #temp_demographic_data
SET town_class_code=NULL
WHERE (state_group_code,town_class_code) in();
*/
END
-- When Only StateGroup Exists
IF #is_stategroup_exist=1 and #is_townclass_exist=0
BEGIN
INSERT INTO #temp_demographic_data
SELECT * FROM ##temp_demographic_data_set
WHERE
(state_group_code in (select code from #temp_state_megacity )) OR
(state_group_code in (select code from #temp_state_10lac )) OR
(state_group_code in (select code from #temp_state_below10)) OR
(state_group_code in (select code from #temp_state_rural)) ;
END
-- When Only Town class Exists
IF #is_stategroup_exist=0 and #is_townclass_exist=1
BEGIN
INSERT INTO #temp_demographic_data
SELECT * FROM ##temp_demographic_data_set;
END
-- When State and Town Both Not exists
IF #is_stategroup_exist=0 and #is_townclass_exist=0
BEGIN
INSERT INTO #temp_demographic_data
SELECT * FROM ##temp_demographic_data_set;
END
END
-- select #temp_query;
-- execute sp_executesql #temp_query;
if object_id('tempdb..##temp_report_data') is not null drop table ##temp_report_data ;
-- if object_id('tempdb..##temp_report_data1') is not null drop table ##temp_report_data1 ;
-- when input limit value is not null then
SET #temp_query='
SELECT *,ROW_NUMBER() OVER (ORDER BY file_date,member_id) as row_number INTO ##temp_report_data
FROM
(
SELECT DISTINCT file_date,member_id,'+#demographic_report+'
FROM
(
SELECT DISTINCT
coalesce(_nccs.name,'''') as master_nccs,
coalesce(_stategroup.name,'''') as master_state_group,
coalesce(CASE WHEN _stategroup_code.is_exposed=0 THEN '''' ELSE _townclass.name END,'''') as master_town_class,
coalesce(_agegroup.name,'''') as master_age_group,
coalesce(CASE WHEN filetered_data.gender=1 THEN ''MALE'' ELSE ''FEMALE'' END,'''')as master_gender,
-- cast(round(filetered_data.weight,2) as char(10)) as weight,filetered_data.household_id,
filetered_data.*
from(
select DISTINCT *
from #temp_demographic_data
)as filetered_data
left join #temp_nccs _nccs_code on filetered_data.nccs_group_code=_nccs_code.code
left join _nccs on _nccs_code.nccs_id=_nccs.id
left join _townclass on filetered_data.town_class_code=_townclass.code
left join #temp_agegroup _agegroup_code on filetered_data.age_group_code=_agegroup_code.code
left join _agegroup on _agegroup_code.agegroup_id=_agegroup.id
left join (
select * from #temp_state_megacity
union
select * from #temp_state_10lac
union
select * from #temp_state_below10
union
select * from #temp_state_rural
)_stategroup_code on filetered_data.state_group_code=_stategroup_code.code
left join _stategroup on _stategroup_code.stategroup_id=_stategroup.id
)report
) report_view';
execute sp_executesql #temp_query ;
CREATE INDEX IX_TEST_temp_temp_report_data ON ##temp_report_data(row_number);
SET #record_count=(SELECT max(row_number) FROM ##temp_report_data);
IF #limt is not null
begin
select *,#record_count as count FROM ##temp_report_data
where row_number between #off_set and #off_set+#limt;
end
-- when input limit value is null
else
begin
select *,#record_count as count FROM ##temp_report_data
END
-- Drop temporary tables
if object_id('tempdb..#temp_demographic_data') is not null drop table #temp_demographic_data ;
if object_id('tempdb..##temp_demographic_data_set') is not null drop table ##temp_demographic_data_set ;
if object_id('tempdb..#temp_agegroup') is not null drop table #temp_agegroup ;
if object_id('tempdb..#temp_gender') is not null drop table #temp_gender ;
if object_id('tempdb..#temp_nccs') is not null drop table #temp_nccs;
if object_id('tempdb..#temp_state_rural') is not null drop table #temp_state_rural ;
if object_id('tempdb..#temp_state_below10') is not null drop table #temp_state_below10 ;
if object_id('tempdb..#temp_state_10lac') is not null drop table #temp_state_10lac ;
if object_id('tempdb..#temp_state_megacity') is not null drop table #temp_state_megacity ;
END
C# Code :
SqlParameter[] mySqlParam = new SqlParameter[19];
mySqlParam[0] = new SqlParameter("_startdate", startDate);
mySqlParam[1] = new SqlParameter("_enddate", endOfDay);
mySqlParam[2] = new SqlParameter("_nccs", nccs == "" ? null : nccs);
mySqlParam[3] = new SqlParameter("_megacity", Convert.ToInt32(ConfigurationManager.AppSettings["megacities"]));
mySqlParam[4] = new SqlParameter("_10lac", Convert.ToInt32(ConfigurationManager.AppSettings["tento75L"]));
mySqlParam[5] = new SqlParameter("_below10", Convert.ToInt32(ConfigurationManager.AppSettings["urban"]));
mySqlParam[6] = new SqlParameter("_rural", Convert.ToInt32(ConfigurationManager.AppSettings["rural"]));
mySqlParam[7] = new SqlParameter("_state_megacity", megaCities == "" ? null : megaCities);
mySqlParam[8] = new SqlParameter("_state_10lac", tenTo75Lac == "" ? null : tenTo75Lac);
mySqlParam[9] = new SqlParameter("_state_below10", urban == "" ? null : urban);
mySqlParam[10] = new SqlParameter("_state_rural", rural == "" ? null : rural);
string gender = null;
if (male == true && female == true)
{
gender = ConfigurationManager.AppSettings["male"] + "," + ConfigurationManager.AppSettings["female"];
}
else if (male == true)
{
gender = ConfigurationManager.AppSettings["male"];
}
else if (female == true)
{
gender = ConfigurationManager.AppSettings["female"];
}
mySqlParam[11] = new SqlParameter("_gender", gender);
mySqlParam[12] = new SqlParameter("_agegroup", ageGroup == "" ? null : ageGroup);
mySqlParam[13] = new SqlParameter("off_set", offSet);
mySqlParam[14] = new SqlParameter("limt", limit);
mySqlParam[15] = new SqlParameter("_Subscription_start", subscription_start);
mySqlParam[16] = new SqlParameter("_Subscription_end", endOfDaySubscription);
mySqlParam[17] = new SqlParameter("_demographic_field_id ", demographicFields);
mySqlParam[18] = new SqlParameter("templateid ", templateId);
DataSet ds = new DataSet();
ds = SqlHelper.ExecuteDataset(GlobalConstants.ConnString, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_get_demographic_history_csv"].ToString(), mySqlParam);
return ds;
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//create & open an SqlConnection, and dispose of it after we are done.
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
//call the overload that takes a connection in place of the connection string
return ExecuteDataset(cn, commandType, commandText, commandParameters);
}
}
Is it possible that, in production, there're instances where the script returns no rows?
select #demographic_report= {...}
from dbo.demgraphic_fields
where is_inventory_field=0 and ( #templateid=0 OR id in (select cast(split_data as int) from ##temp_convert));
If this is the case, #demographic_report remains null, 'any string' + null is null, sp_executesql is executed against a null string that does nothing, but the table is not created and this causes your error.
Some notes:
ExecuteNonQuery returns -1
ExecuteNonQuery will drop the table (#droptable), but it will not create the new table (#code)
the length of the #code query is 10265 characters
The stored procedure runs perfectly fine in SSMS and returns 22 rows in the table
Are there any ideas as to why C#'s ExecuteNonQuery function doesn't seem to be executing the 'exec(#code)' portion of the stored procedure?
ALTER procedure [dbo].[sp_create_EditControlResultsPivot]
as
begin
declare #t nvarchar (250);
set #t = 'editControlResults'
declare #newtable nvarchar(250);
set #newtable = 'dbo.' + #t + 'Pivot'
declare #nonPivotColumn1 nvarchar(250);
set #nonPivotColumn1 = 'num'
declare #nonPivotColumn2 nvarchar(25);
set #nonPivotColumn2 = 'File_Name'
declare #droptable nvarchar(max);
set #droptable =
'if EXISTS (select * from sys.objects where object_id = object_id(N''' + #newtable + '''))
begin drop table ' + #newtable + ' end
'
declare #i int
set #i = 1;
declare #itemList nvarchar(max);
declare #code nvarchar(max);
while #i <= (
select COUNT(*)
from sys.columns c
left join sys.tables t on c.object_id = t.object_id
where 1=1
and c.name not like #nonPivotColumn1
and c.name not like #nonPivotColumn2
and t.name = #t
)
begin
set #itemList = #itemList + ', ' +
(
select col from
(
select c.name as col, ROW_NUMBER () over (order by c.name) as num from
sys.columns c left join sys.tables t on c.object_id = t.object_id
where 1=1
and c.name not like #nonPivotColumn1
and c.name not like #nonPivotColumn2
and t.name = #t
) sub where num = #i
)
set #i = #i + 1
end
set #itemList = (select substring(#itemList, 2, LEN(#itemList)))
set #code = '
SELECT ' + #nonpivotcolumn2 + ', Item
into ' + #newtable + '
FROM
(SELECT ' + #nonpivotcolumn2 + ', ' + #itemList + '
FROM ' + #t + ') sub
UNPIVOT
(Value FOR Item IN (' + #itemList + ')
) AS sub
where Value = ''true''
'
exec(#droptable)
exec(#code);
--print(len(#code))
END
--exec sp_create_EditControlResultsPivot
The ExecuteNonQuery Method returns the number of rows affected use the ExecuteReader method instead.
SqlCommand.ExecuteReader Method
The only way to return data from ExecuteNonQuery would be via an Output parameter.
I suspect your comment #3. the length of the #code query is 10265 characters...could be an issue...I think the call from C# is chopping it to only 4000 or 8000 chars...
Since you are not expecting a resultset, ExecuteNonQuery is good.
Things to try:
Try inserting the content of the #code variable (inside the procedure) in a table and see if you are getting the correct sql...both when executed from SSMS and from C# call
If you get a valid sql query in step 1 (which I doubt)...try executing that query in SSMS to see if it really works...
I'm looking at creating a basic ORM (purely for fun), and was wondering, is there a way to return the list of tables in a database and also the fields for every table?
Using this, I want to be able to loop through the result set (in C#) and then say for each table in the result set, do this (e.g. use reflection to make a class that will do or contain xyz).
Further to this, what are some good online blogs for SQL Server? I know this question is really about using system SPs and databases in Sql Server, and I am ok with general queries, so I'm interested in some blogs which cover this sort of functionality.
Thanks
Is this what you are looking for:
Using OBJECT CATALOG VIEWS
SELECT T.name AS Table_Name ,
C.name AS Column_Name ,
P.name AS Data_Type ,
C.max_length AS Size ,
CAST(P.precision AS VARCHAR) + '/' + CAST(P.scale AS VARCHAR) AS Precision_Scale
FROM sys.objects AS T
JOIN sys.columns AS C ON T.object_id = C.object_id
JOIN sys.types AS P ON C.system_type_id = P.system_type_id
WHERE T.type_desc = 'USER_TABLE';
Using INFORMATION SCHEMA VIEWS
SELECT TABLE_SCHEMA ,
TABLE_NAME ,
COLUMN_NAME ,
ORDINAL_POSITION ,
COLUMN_DEFAULT ,
DATA_TYPE ,
CHARACTER_MAXIMUM_LENGTH ,
NUMERIC_PRECISION ,
NUMERIC_PRECISION_RADIX ,
NUMERIC_SCALE ,
DATETIME_PRECISION
FROM INFORMATION_SCHEMA.COLUMNS;
Reference : My Blog - http://dbalink.wordpress.com/2008/10/24/querying-the-object-catalog-and-information-schema-views/
Tables ::
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
columns ::
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
or
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name'
Get list of all the tables and the fields in database:
Select *
From INFORMATION_SCHEMA.COLUMNS
Where TABLE_CATALOG Like 'DatabaseName'
Get list of all the fields in table:
Select *
From INFORMATION_SCHEMA.COLUMNS
Where TABLE_CATALOG Like 'DatabaseName' And TABLE_NAME Like 'TableName'
I tested a few solutions an found that
Select *
From INFORMATION_SCHEMA.COLUMNS
gives you the column info for your CURRENT/default database.
Select *
From <DBNAME>.INFORMATION_SCHEMA.COLUMNS
, without the < and >, gives you the column info for the database DBNAME.
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
Your other inbuilt friend here is the system sproc SP_HELP.
sample usage ::
sp_help <MyTableName>
It returns a lot more info than you will really need, but at least 90% of your possible requirements will be catered for.
Just throwing this out there - easy to now copy/paste into a word or google doc:
PRINT '<html><body>'
SET NOCOUNT ON
DECLARE #tableName VARCHAR(30)
DECLARE tableCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT T.name AS TableName
FROM sys.objects AS T
WHERE T.type_desc = 'USER_TABLE'
ORDER BY T.name
OPEN tableCursor
FETCH NEXT FROM tableCursor INTO #tableName
WHILE ##FETCH_STATUS = 0 BEGIN
PRINT '<h2>' + #tableName + '</h2>'
PRINT '<pre>'
SELECT LEFT(C.name, 30) AS ColumnName,
LEFT(ISC.DATA_TYPE, 10) AS DataType,
C.max_length AS Size,
CAST(P.precision AS VARCHAR(4)) + '/' + CAST(P.scale AS VARCHAR(4)) AS PrecScale,
CASE WHEN C.is_nullable = 1 THEN 'Null' ELSE 'No Null' END AS [Nullable],
LEFT(ISNULL(ISC.COLUMN_DEFAULT, ' '), 5) AS [Default],
CASE WHEN C.is_identity = 1 THEN 'Identity' ELSE '' END AS [Identity]
FROM sys.objects AS T
JOIN sys.columns AS C ON T.object_id = C.object_id
JOIN sys.types AS P ON C.system_type_id = P.system_type_id
JOIN INFORMATION_SCHEMA.COLUMNS AS ISC ON T.name = ISC.TABLE_NAME AND C.name = ISC.COLUMN_NAME
WHERE T.type_desc = 'USER_TABLE'
AND T.name = #tableName
ORDER BY T.name, ISC.ORDINAL_POSITION
PRINT '</pre>'
FETCH NEXT FROM tableCursor INTO #tableName
END
CLOSE tableCursor
DEALLOCATE tableCursor
SET NOCOUNT OFF
PRINT '</body></html>'
SELECT * FROM INFORMATION_SCHEMA.COLUMNS for get all
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS for get all table name.
Try it on sqlserver,
This will get you all the user created tables:
select * from sysobjects where xtype='U'
To get the cols:
Select * from Information_Schema.Columns Where Table_Name = 'Insert Table Name Here'
Also, I find http://www.sqlservercentral.com/ to be a pretty good db resource.
This will return the database name, table name, column name and the datatype of the column specified by a database parameter:
declare #database nvarchar(25)
set #database = ''
SELECT cu.table_catalog,cu.VIEW_SCHEMA, cu.VIEW_NAME, cu.TABLE_NAME,
cu.COLUMN_NAME,c.DATA_TYPE,c.character_maximum_length
from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE as cu
JOIN INFORMATION_SCHEMA.COLUMNS as c
on cu.TABLE_SCHEMA = c.TABLE_SCHEMA and c.TABLE_CATALOG =
cu.TABLE_CATALOG
and c.TABLE_NAME = cu.TABLE_NAME
and c.COLUMN_NAME = cu.COLUMN_NAME
where cu.TABLE_CATALOG = #database
order by cu.view_name,c.COLUMN_NAME
For MYSQL:
Select *
From INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = "<DatabaseName>"
I found an easy way to fetch the details of Tables and columns of a particular DB using SQL developer.
Select *FROM USER_TAB_COLUMNS
in a Microsoft SQL Server you can use this:
declare #sql2 nvarchar(2000)
set #sql2 ='
use ?
if ( db_name(db_id()) not in (''master'',''tempdb'',''model'',''msdb'',''SSISDB'') )
begin
select
db_name() as db,
SS.name as schemaname,
SO.name tablename,
SC.name columnname,
ST.name type,
case when ST.name in (''nvarchar'', ''nchar'')
then convert(varchar(10), ( SC.max_length / 2 ))
when ST.name in (''char'', ''varchar'')
then convert(varchar(10), SC.max_length)
else null
end as length,
case when SC.is_nullable = 0 then ''No'' when SC.is_nullable = 1 then ''Yes'' else null end as nullable,
isnull(SC.column_id,0) as col_number
from sys.objects SO
join sys.schemas SS
on SS.schema_id = SO.schema_id
join sys.columns SC
on SO.object_id = SC.object_id
left join sys.types ST
on SC.user_type_id = ST.user_type_id and SC.system_type_id = ST.system_type_id
where SO.is_ms_shipped = 0
end
'
exec sp_msforeachdb #command1 = #sql2
this shows you all tables and columns ( and their definition ) from all userdefined databases.