Showing posts with label selection. Show all posts
Showing posts with label selection. Show all posts

Thursday, March 29, 2012

Dynamic selection of flat file

I am writing a package where the user uploads a flat file to a web folder. I need to automate this package to run everytime it sees a new file.

How can I implement this?

Can I make a call to a package or a sql server job to run from .net 2.0?

Do I need to use a service broker to look for a new file and run the package or a stored proc....I am looking for an async process where user doesnt have to wait for the package to run as it involves data validation of flat file and its huge...

Please help!!

How about a scheduled package that runs every minute, and if it finds a file it runs the load task, otherwise it just ends.

You could do something event driven with the WMI event task, but I dislike that since it does not tell you what file has been found, or try the File Watcher Task (http://www.sqlis.com/default.aspx?23)

|||

Appreciate your response...

I was wondering about creating a config file in the database and update the Connection string value everytime a new file is uploaded with the new file name

And then call the package to run by adding dts assembly to visual studio and doing package.load....

Any suggestion or comments on this as I really dont want to run this every min....

Thursday, March 22, 2012

Dynamic Parameters selection

this might be a stupid question.

I have a parameter which has a default value set to it. when a second value is selexcted is it necessary to click the view report button.

Could it be set to auto reload on dropdown selection.

I am using reporting Services 2000

Thanks.

Hey guys,

Would there be a runtime load function available for the above problem.

thanks

Wednesday, March 21, 2012

Dynamic measures

Hi,

I am new to mdx. Based on the requirement, I need to dynamically loaded up a column of measures depending on the selection of a parameter.

The parameter values has, Actual, Budget, Target

For the one field, base on the above parameter, will select,

if the value for the parameter is Actual, then we will have only a column of Actual values

if it's Budget, then we will have only a column of Budget values

if it's Target, then we will have only a column of Target values

How should I write the mdx query for this?

I'm really desperate for the answer.

Thanks a lot for your help.

The recommended way to do this is to create a seperate scenario dimension that jsut contains the members Actual, Budget, and Target. Then depending on which of theses members you have selected in your query, you will see the appropriate values in your measures.sql

Dynamic measures

Hi,

I am new to mdx. Based on the requirement, I need to dynamically loaded up a column of measures depending on the selection of a parameter.

The parameter values has, Actual, Budget, Target

For the one field, base on the above parameter, will select,

if the value for the parameter is Actual, then we will have only a column of Actual values

if it's Budget, then we will have only a column of Budget values

if it's Target, then we will have only a column of Target values

How should I write the mdx query for this?

I'm really desperate for the answer.

Thanks a lot for your help.

The recommended way to do this is to create a seperate scenario dimension that jsut contains the members Actual, Budget, and Target. Then depending on which of theses members you have selected in your query, you will see the appropriate values in your measures.

Friday, March 9, 2012

Dynamic field base on parameter

Hi,

I have a matrix based on a cube. I would like to load up a field based on the selection of a parameter.

The parameter values has, Actual, Budget, Target

For the one field, base on the above parameter, will select,

if the value for the parameter is Actual, then =Fields!Actual.Value

if it's Budget, then =Fields!Budget.Value

if it's Target, then =Fields!Target.Value

Is this possible? And If so, how should I do it?

Thanks a lot.

You can try

=IIF(Parameters!<ParamName>.Value="Actual", Fields!Actual.Value, IIF(Parameters!<ParamName>.Value="Budget", Fields!Budget.Value, Fields!Target.Value))

|||

hi,

thanks a lot for your reply.

But what if I loaded the parameter list from the database, that means there maybe more option later in the course, what can I do in that case?

|||You can try using this syntax =Fields(Parameters!<paramName>.Value).Value.|||thanks a lot for your great help... that works!

Wednesday, March 7, 2012

Dynamic Dataset to Reports

Hi,
I have some selection criteria in my .aspx web page and based on the
selections, by using an stored procedure, I am generating the dataset. As of
now, I am displaying this in HTML table format.
Now I have designed the report using Reporting Services and have deployed it
onto the Report Server. I would like to know how do I pass this dataset from
my .aspx webpage onto the report and generate the same.
Could any one please let me know the links to materials explaining this
technique or any help would be highly appreciated.
Regards,
Sudhakara.T.P.Hi,
Try to do this.
1. Convert your RDL files in RLDC files (see this topic at VS2005 help).
2. Configure your ReportViewer to a LocalReport mode. This will allow you to
link your dataset to a report at runtime by using ReportViewer.LocalReport
methods or at disign level too.
Hopefully I help you.
Arturo Carrión
at Teimpo Hard Development team
"Sudhakara.T.P." <SudhakaraTP@.discussions.microsoft.com> escribió en el
mensaje news:3188FD41-0A7F-448F-AE2C-7AD2423F343F@.microsoft.com...
> Hi,
> I have some selection criteria in my .aspx web page and based on the
> selections, by using an stored procedure, I am generating the dataset. As
> of
> now, I am displaying this in HTML table format.
> Now I have designed the report using Reporting Services and have deployed
> it
> onto the Report Server. I would like to know how do I pass this dataset
> from
> my .aspx webpage onto the report and generate the same.
> Could any one please let me know the links to materials explaining this
> technique or any help would be highly appreciated.
> Regards,
> Sudhakara.T.P.|||Hi,
Thank you very much for your reply.
Infact, I tried doing this earlier, but the problem with this is that, in
the preview mode, I am not getting the print button to print the button, even
tough I have set the showprintoption to true.
Any alternative solution for this?
Regards,
Sudhakara.T.P.
"tiempotecnologia@.newsgroup.nospam" wrote:
> Hi,
> Try to do this.
> 1. Convert your RDL files in RLDC files (see this topic at VS2005 help).
> 2. Configure your ReportViewer to a LocalReport mode. This will allow you to
> link your dataset to a report at runtime by using ReportViewer.LocalReport
> methods or at disign level too.
> Hopefully I help you.
> Arturo Carrión
> at Teimpo Hard Development team
> "Sudhakara.T.P." <SudhakaraTP@.discussions.microsoft.com> escribió en el
> mensaje news:3188FD41-0A7F-448F-AE2C-7AD2423F343F@.microsoft.com...
> > Hi,
> > I have some selection criteria in my .aspx web page and based on the
> > selections, by using an stored procedure, I am generating the dataset. As
> > of
> > now, I am displaying this in HTML table format.
> > Now I have designed the report using Reporting Services and have deployed
> > it
> > onto the Report Server. I would like to know how do I pass this dataset
> > from
> > my .aspx webpage onto the report and generate the same.
> > Could any one please let me know the links to materials explaining this
> > technique or any help would be highly appreciated.
> > Regards,
> > Sudhakara.T.P.
>
>

Dynamic database selection?

I'm working on a script to convert data from one software package
to another. Greatly simplified, it looks something like

create procedure import_widget as
begin

insert into our_widget (foo, bar)
select baz, quux from their_db.dbo.their_widget

end
go

The problem is that the name of the source database varies from
one system to another, so I want to pass the database name as a
parameter. I think I could do the following, but is there a
better way to go about it?

create procedure import_widget (@.db_name sysname) as
begin

exec 'create view their_widget as select * from '
+ @.db_name + '.dbo.their_widget'

insert into our_widget (foo, bar)
select baz, quux from their_widget

drop view their_widget

end
goEd Murphy (emurphy42@.socal.rr.com) writes:

Quote:

Originally Posted by

The problem is that the name of the source database varies from
one system to another, so I want to pass the database name as a
parameter. I think I could do the following, but is there a
better way to go about it?


On SQL 2005 you could use synonyms:

CREATE SYNONYM mytable AS thatdatabase.dbo.hertable

When you move to a new database you only need to update the synonyms.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland Sommarskog wrote:

Quote:

Originally Posted by

Ed Murphy (emurphy42@.socal.rr.com) writes:

Quote:

Originally Posted by

>The problem is that the name of the source database varies from
>one system to another, so I want to pass the database name as a
>parameter. I think I could do the following, but is there a
>better way to go about it?


>
On SQL 2005 you could use synonyms:
>
CREATE SYNONYM mytable AS thatdatabase.dbo.hertable
>
When you move to a new database you only need to update the synonyms.


Alas, this is SQL 2000 (or at least I expect it will be in a
significant number of cases).|||Ed Murphy (emurphy42@.socal.rr.com) writes:

Quote:

Originally Posted by

Erland Sommarskog wrote:
>

Quote:

Originally Posted by

>Ed Murphy (emurphy42@.socal.rr.com) writes:

Quote:

Originally Posted by

>>The problem is that the name of the source database varies from
>>one system to another, so I want to pass the database name as a
>>parameter. I think I could do the following, but is there a
>>better way to go about it?


>>
>On SQL 2005 you could use synonyms:
>>
> CREATE SYNONYM mytable AS thatdatabase.dbo.hertable
>>
>When you move to a new database you only need to update the synonyms.


>
Alas, this is SQL 2000 (or at least I expect it will be in a
significant number of cases).


Then the best may be to have a stored procedure in the other database
to retreive that data. You still need to construct the procedure name
dynamically, but since EXEC accepts a variable for the procedure name,
you don't have to use dynamic SQL.

That is you can say:

SELECT @.sp_name = @.dbname + '..that_sp'
EXEC @.sp_name

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Sunday, February 26, 2012

dynamic creation of select statement

hello,

My basic requirement is i have five drop down list where data is selected and based on the selection the no. of colums to be selected have to be designed. ex : say i have following drop down lists:
1. community
2. gender
3. level
4. term
5. year

at runtime is it possible to design the crystal reports columns.

Pl. help out in directing to url or given sample source to dynamic creation of columns in the crystal reports.

thanks in advance
karunau need to add the formulas and the field object formatting.
u can do this by declaring an array -->
IFormulaFieldDefinitionPtr formulas[10];

then when adding formulas-->
ISectionPtr pSection = GetReportSection(sectionNum);
IFormulaFieldDefinitionsPtr pFormulaFields = 0;
m_Report->get_FormulaFields((IFormulaFieldDefinitions**) &pFormulaFields);

CString Recur = CString(L"WhileReadingRecords;" +
(CString)__toascii(13) +
(CString)__toascii(10));

// Add the formulas to the formula fields collection
CString CStrText = Recur + CString(L"Space(10)");
BSTR strText = CStrText.AllocSysString();
BSTR objName;
int i=0;
////assume u have stored the field names somewhere like a list or an array.

for(iter=lstRecs.begin();iter!=lstRecs.end();iter++)
{

CString str= *iter;
if(i==0)
{
objName = str.AllocSysString();
formulas[i]=pFormulaFields->Add(objName, strText);


}
else
{
SysReAllocString(&objName, str.AllocSysString());
formulas[i]=pFormulaFields->Add(objName, strText);

}

i++;
}

////////////////////////////////////////////////////////////////////////////////
then add the field objects-->

ISectionPtr pSection = GetReportSection(3);
IFieldObjectPtr pFieldObj = 0;

LocX = 10; // Horizontal offset
VARIANT var;
VariantInit (&var);
var.vt = VT_DISPATCH;
BSTR objName = NULL;

int i=0;

for(iter=lstRecs.begin();iter!=lstRecs.end();iter++)
{

CString str= *iter;
var.pdispVal = formulas[i];
pFieldObj = pSection->AddFieldObject(var, LocX, 0);
SysReAllocString(&objName,str.AllocSysString());
pFieldObj->put_Name(objName);
pFieldObj->put_Width(1600);
LocX += 1500;
}

////////////////////////////////////////////////////////////////////////////////////////////
thats it! hope this helps...

Friday, February 24, 2012

Dynamic columns in RS reports

I have to design a report that will have a dynamic number of columns, in a
dynamic order.
For example: items and a selection from their attributes
ITEM Attr 1 Attr 2
Attr3 Attr 4
I1 5 Realy large text
........................................ Short text
12305
etc...
I know the matrix is used to obtain dynamic number of columns, but the
problem is that the columns in matrix have the same width
and i need different column widths (for text attributes and numeric
attributes for example), and the column width can have only numeric
(and fixed) values - i mean you can put there a function call..
Is there any way other than generating the rdl at run time to obtain such
dynamic table ?
Thanks in advance for any advice.This is not really feasible as far as I can tell, in the way you are
asking.
You can't set column width dynamically, which sounds like a big problem
for you.
But you may be able to get some way towards it if you compromise a
little.
In terms of no. of columns, you'd need to set a maximum number, make
sure you always return that number in the dataset even if they contain
nulls you can then allow for the maximum in your report and just hide
or show them as needed. You'd have to do it this way because you will
be referring to the columns with a field name and the report validates
that those fields will be returned when it renders.
You can then hide table columns or text boxes in a list control based
on the number of columns you're expecting that scenario. You could have
several table columns geared to one dataset column, all set differently
according to data type, then hide the columns that you don't need based
on whats returned in the query.
Chris
Razvan Popov wrote:
> I have to design a report that will have a dynamic number of columns,
> in a dynamic order.
> For example: items and a selection from their attributes
> ITEM Attr 1 Attr 2
> Attr3 Attr 4
> I1 5 Realy large text
> ........................................ Short text
> 12305
> etc...
> I know the matrix is used to obtain dynamic number of columns, but
> the problem is that the columns in matrix have the same width
> and i need different column widths (for text attributes and numeric
> attributes for example), and the column width can have only numeric
> (and fixed) values - i mean you can put there a function call..
> Is there any way other than generating the rdl at run time to obtain
> such dynamic table ?
> Thanks in advance for any advice.|||Thank you for your suggestion. But my problem is a little worse than that:
I don't have a maximum number of columns (the user can add attributes and
select them into the report at runtime).
And if i set a maximum number of X columns as constraint. I don't know which
will be the X attributes the user selected to
be displayed into report and for that I have no way of presetting the width
of the columns (which should be dependent on attribute type)..
I have as the last option to generate dinamically the RDL (or parts of it)
but this is prety complex solution and i would like to be sure that there is
no
simpler solution before i start coding such scenario...
Thx
"Chris McGuigan" <chris.mcguigan@.zycko.com> wrote in message
news:u%23XoXUncFHA.2436@.TK2MSFTNGP10.phx.gbl...
> This is not really feasible as far as I can tell, in the way you are
> asking.
> You can't set column width dynamically, which sounds like a big problem
> for you.
> But you may be able to get some way towards it if you compromise a
> little.
> In terms of no. of columns, you'd need to set a maximum number, make
> sure you always return that number in the dataset even if they contain
> nulls you can then allow for the maximum in your report and just hide
> or show them as needed. You'd have to do it this way because you will
> be referring to the columns with a field name and the report validates
> that those fields will be returned when it renders.
> You can then hide table columns or text boxes in a list control based
> on the number of columns you're expecting that scenario. You could have
> several table columns geared to one dataset column, all set differently
> according to data type, then hide the columns that you don't need based
> on whats returned in the query.
> Chris|||Razvan,
You've got a pretty complex scenario, I think you're going to need a
complex solution!
It sounds like you're developing a form of end-user report builder?
Have you taken a look at Cizer (www.cizer.com) or Report Builder in
SQL2005?
Chris
Razvan Popov wrote:
> Thank you for your suggestion. But my problem is a little worse than
> that: I don't have a maximum number of columns (the user can add
> attributes and select them into the report at runtime).
> And if i set a maximum number of X columns as constraint. I don't
> know which will be the X attributes the user selected to
> be displayed into report and for that I have no way of presetting the
> width of the columns (which should be dependent on attribute type)..
> I have as the last option to generate dinamically the RDL (or parts
> of it) but this is prety complex solution and i would like to be sure
> that there is no
> simpler solution before i start coding such scenario...
> Thx
>
> "Chris McGuigan" <chris.mcguigan@.zycko.com> wrote in message
> news:u%23XoXUncFHA.2436@.TK2MSFTNGP10.phx.gbl...
> > This is not really feasible as far as I can tell, in the way you are
> > asking.
> > You can't set column width dynamically, which sounds like a big
> > problem for you.
> >
> > But you may be able to get some way towards it if you compromise a
> > little.
> > In terms of no. of columns, you'd need to set a maximum number, make
> > sure you always return that number in the dataset even if they
> > contain nulls you can then allow for the maximum in your report and
> > just hide or show them as needed. You'd have to do it this way
> > because you will be referring to the columns with a field name and
> > the report validates that those fields will be returned when it
> > renders.
> >
> > You can then hide table columns or text boxes in a list control
> > based on the number of columns you're expecting that scenario. You
> > could have several table columns geared to one dataset column, all
> > set differently according to data type, then hide the columns that
> > you don't need based on whats returned in the query.
> >
> > Chris|||Thank you for your help..
If i find any solution simpler than generating rdls dynamically i'll post it
here..
Razvan

Sunday, February 19, 2012

Dynamic columns in RS reports

I have to design a report that will have a dynamic number of columns, in a
dynamic order.
For example: items and a selection from their attributes
ITEM Attr 1 Attr 2
Attr3 Attr 4
I1 5 Realy large text
........................................ Short text
12305
etc...
I know the matrix is used to obtain dynamic number of columns, but the
problem is that the columns in matrix have the same width
and i need different column widths (for text attributes and numeric
attributes for example), and the column width can have only numeric
(and fixed) values - i mean you can put there a function call..
Is there any way other than generating the rdl at run time to obtain such
dynamic table ?
Thanks in advance for any advice.This is not really feasible as far as I can tell, in the way you are
asking.
You can't set column width dynamically, which sounds like a big problem
for you.
But you may be able to get some way towards it if you compromise a
little.
In terms of no. of columns, you'd need to set a maximum number, make
sure you always return that number in the dataset even if they contain
nulls you can then allow for the maximum in your report and just hide
or show them as needed. You'd have to do it this way because you will
be referring to the columns with a field name and the report validates
that those fields will be returned when it renders.
You can then hide table columns or text boxes in a list control based
on the number of columns you're expecting that scenario. You could have
several table columns geared to one dataset column, all set differently
according to data type, then hide the columns that you don't need based
on whats returned in the query.
Chris
Razvan Popov wrote:
> I have to design a report that will have a dynamic number of columns,
> in a dynamic order.
> For example: items and a selection from their attributes
> ITEM Attr 1 Attr 2
> Attr3 Attr 4
> I1 5 Realy large text
> ........................................ Short text
> 12305
> etc...
> I know the matrix is used to obtain dynamic number of columns, but
> the problem is that the columns in matrix have the same width
> and i need different column widths (for text attributes and numeric
> attributes for example), and the column width can have only numeric
> (and fixed) values - i mean you can put there a function call..
> Is there any way other than generating the rdl at run time to obtain
> such dynamic table ?
> Thanks in advance for any advice.|||Thank you for your suggestion. But my problem is a little worse than that:
I don't have a maximum number of columns (the user can add attributes and
select them into the report at runtime).
And if i set a maximum number of X columns as constraint. I don't know which
will be the X attributes the user selected to
be displayed into report and for that I have no way of presetting the width
of the columns (which should be dependent on attribute type)..
I have as the last option to generate dinamically the RDL (or parts of it)
but this is prety complex solution and i would like to be sure that there is
no
simpler solution before i start coding such scenario...
Thx
"Chris McGuigan" <chris.mcguigan@.zycko.com> wrote in message
news:u%23XoXUncFHA.2436@.TK2MSFTNGP10.phx.gbl...
> This is not really feasible as far as I can tell, in the way you are
> asking.
> You can't set column width dynamically, which sounds like a big problem
> for you.
> But you may be able to get some way towards it if you compromise a
> little.
> In terms of no. of columns, you'd need to set a maximum number, make
> sure you always return that number in the dataset even if they contain
> nulls you can then allow for the maximum in your report and just hide
> or show them as needed. You'd have to do it this way because you will
> be referring to the columns with a field name and the report validates
> that those fields will be returned when it renders.
> You can then hide table columns or text boxes in a list control based
> on the number of columns you're expecting that scenario. You could have
> several table columns geared to one dataset column, all set differently
> according to data type, then hide the columns that you don't need based
> on whats returned in the query.
> Chris|||Razvan,
You've got a pretty complex scenario, I think you're going to need a
complex solution!
It sounds like you're developing a form of end-user report builder?
Have you taken a look at Cizer (www.cizer.com) or Report Builder in
SQL2005?
Chris
Razvan Popov wrote:
> Thank you for your suggestion. But my problem is a little worse than
> that: I don't have a maximum number of columns (the user can add
> attributes and select them into the report at runtime).
> And if i set a maximum number of X columns as constraint. I don't
> know which will be the X attributes the user selected to
> be displayed into report and for that I have no way of presetting the
> width of the columns (which should be dependent on attribute type)..
> I have as the last option to generate dinamically the RDL (or parts
> of it) but this is prety complex solution and i would like to be sure
> that there is no
> simpler solution before i start coding such scenario...
> Thx
>
> "Chris McGuigan" <chris.mcguigan@.zycko.com> wrote in message
> news:u%23XoXUncFHA.2436@.TK2MSFTNGP10.phx.gbl...
> > This is not really feasible as far as I can tell, in the way you are
> > asking.
> > You can't set column width dynamically, which sounds like a big
> > problem for you.
> >
> > But you may be able to get some way towards it if you compromise a
> > little.
> > In terms of no. of columns, you'd need to set a maximum number, make
> > sure you always return that number in the dataset even if they
> > contain nulls you can then allow for the maximum in your report and
> > just hide or show them as needed. You'd have to do it this way
> > because you will be referring to the columns with a field name and
> > the report validates that those fields will be returned when it
> > renders.
> >
> > You can then hide table columns or text boxes in a list control
> > based on the number of columns you're expecting that scenario. You
> > could have several table columns geared to one dataset column, all
> > set differently according to data type, then hide the columns that
> > you don't need based on whats returned in the query.
> >
> > Chris|||Thank you for your help..
If i find any solution simpler than generating rdls dynamically i'll post it
here..
Razvan

Dynamic Column Selection in Stored Procedure

Hey Guys,

Here is the issue I'm having. I am writing a stored procedure that takes a couple of parameters. Each one is the value within a specific column in one table (i.e., @.part = 'o-ring' or @.sub_assembly = 'hydraulic ram'). Needless to say, the columns form a hierarchy. What I am trying to achieve is to allow the user to specify one of the parameters and get a count for all records where the specified value is in the corresponding column. So, if the user puts in the parameter @.part = 'o-ring', I want it to know that the where clause for the select statement should look for o-ring in the part column and not the sub_assembly column. Here is what I am trying to do, which isn't working.

DECLARE @.querycolumn varchar(20),
@.queryvalue varchar(35)

SET @.querycolumn = ''
SET @.queryvalue = ''

IF(@.sub_assembly = NULL)
BEGIN
IF(@.part = NULL)
BEGIN
PRINT 'This is an error. You must have at least a part'
END
ELSE
BEGIN
SET @.querycolumn = 'Part'
SET @.queryvalue = @.part
END
END
ELSE
BEGIN
SET @.querycolumn = 'SubAssembly'
SET @.queryvalue = @.sub_assembly
END

SELECT SubAssembly, Part, COUNT(RecordID)
FROM Table
WHERE @.querycolumn = @.queryvalue
GROUP BY SubAssembly, Part
ORDER BY SubAssembly, Part

The problem is that I'm getting an error when I try to use @.querycolumn to supply the column name to the WHERE clause. Any ideas or suggestions?you should build a command string and then execute it by EXEC()

try this:

DECLARE @.querycolumn varchar(20), @.queryvalue varchar(35), @.command varchar(200)

SET @.command = ''
...
...
...
SET @.command = 'SELECT SubAssembly, Part, COUNT(RecordID) FROM Table
WHERE '+@.querycolumn+' = '''+@.queryvalue+''' GROUP BY SubAssembly, Part
ORDER BY SubAssembly, Part'

EXEC(@.command)

it should help...|||How About:

Declare @.subAssembly nvarchar(215), @.queryColumn nvarchar(216), @.part nvarchar(214)
Declare @.t Table( RecordId int identity(1,1), Part nvarchar(215), SubAssembly nvarchar(214) )

Select @.part = Null --N'Part A'
, @.subAssembly = N'Sub Zero'

Insert @.t (Part, SubAssembly)
Select 'Part A', 'Sub A'
Union
Select 'Part B', 'Sub B'
Union
Select 'Part A', 'Sub Zero'
Union
Select 'Part B', 'Sub Zero'

Select Case
When @.part Is Not Null Then Part
End As 'Part'
, Case
When @.subAssembly Is Not Null Then SubAssembly
End As 'SubAssembly'
, Count(RecordId) As 'Count'
From @.t
Where part = Coalesce(@.part, part)
And SubAssembly = Coalesce(@.SubAssembly, SubAssembly)
Group By Case
When @.part Is Not Null Then Part
End
, Case
When @.subAssembly Is Not Null Then SubAssembly
End

Select * From @.t|||I forgot to mention that the select statement is going to be used in a cursor.

DECLARE generic_cursor CURSOR FOR
EXEC(@.command)

throws an error|||Good lord why?

Gonna make the FETCH dynamic as well?

You have to understand that dynamic sql executes in a separate spid...so your sproc won't access to that session, and it will disappear when the execution is complete.

What are you actually trying to do?|||...and throwing a cursor into the mess too...|||Just say no to cursors.|||Ok, I'll take a stab at this... I'd start with something like:SELECT Count(*)
FROM (
SELECT
p.SubAssembly, p.Part
FROM Table AS p
WHERE p.Part = @.part
UNION SELECT
s.SubAssembly, s.Part
FROM Table AS s
WHERE s.SubAssembly = @.SubAssembly
) AS zIt makes no sense to me to try grouping, since the groups will make no sense at all. This code avoids cursors, although it could be used to create one (although why you'd do that is beyond me). It also uses the UNION to sidestep the need for dynamic code too.

The question is, will it help tianmingqing at all?

-PatP|||Okay, so what I am trying to accomplish here is to create a stored procedure (main routine) that calls another stored procedure (sub routine) that creates a cursor based on parameters passed in by the main routine. I was trying to make that cursor creation dynamic so that I didn't have to write four versions of the same query where only one line in the where clause changes. Maybe I need to rethink this...|||...but why the cursor?

Even mentioning cursors on this forum starts raising concerns, because 9 time out of 10 (at least) they are not necessary and just complicate the code.|||because I need to take each row from the cursor one at a time and compare it with the results from another query that uses the same parameters to make sure that the counts match. If they don't, then I need to return the two counts, and the parameter values so that I know where I have a problem in a process I am running. Thus the reason for the cursor is to iterate through a results set. Is that not a good use for a cursor, and why the disdain for them? Sorry, most of my previous SQL work has been done in relative isolation with help from BOL, so I'm somewhat of a newbie.|||Cursors are really leftovers from previous technologies. They are effectively hang-ons from the logic of unit-record equipment where everything had to be handled one card at a time.

Other database products still use cursors heavily. As a matter of fact, some database engines still rely on cursors for nearly everything that they do.

There is nothing intrinsicly wrong with cursors. As a matter of fact, they are still the only practical way to handle a few tasks such as mass-renaming objects, managing logins, etc.

However, from a performance perspective cursors are at least one order of magnitude slower than similar set-based solutions to the same problem. They are often much worse than that!

For your problem, a simple join of the two queries would get you exactly what you've asked for, with lots less code, and would be much more efficient than using cursors to do the same task.

This begs my original question from my previous post... Did the solution I suggested help you?

-PatP|||...but why the cursor?

Even mentioning cursors on this forum starts raising concerns, because 9 time out of 10 (at least)

More...9.99999|||It would be interesting to keep stats on this.

Raise your hand if you have ever found a cursor in somebody else's code and the use of a cursor actually WAS required...

Hmm...I'm not smelling a lot of armpits...|||Okay, so due to the overwhelming suggestions go forego the cursor, I decided to reconsider what I was attempting and this is what I have come up with.

CREATE PROCEDURE some_proc_name
@.part = NULL,
@.sub_assembly = NULL
AS

CREATE TABLE #temp_table1
( t1_count int,
t2_count int,
part varchar(35),
subAssembly varchar(35)
)

CREATE TABLE #temp_table2
( t2_count int,
part varchar(35),
subAssembly(35)
)

IF(@.sub_assembly = NULL)
BEGIN
IF(@.part = NULL)
BEGIN
PRINT 'This is an error. You must have at least a part'
END
ELSE
BEGIN
INSERT INTO #temp_table1(t1_count, part, subAssembly)
SELECT COUNT(RecordID), part, subAssembly
FROM some_table
WHERE part = @.part
GROUP BY part, subAssembly

INSERT INTO #temp_table2(t2_count, part, subAssembly)
SELECT COUNT(RecordID), part, subAssembly
FROM some_other_table
WHERE part = @.part
GROUP BY part, subAssembly
END
END
ELSE
BEGIN
INSERT INTO #temp_table1(t1_count, part, subAssembly)
SELECT COUNT(RecordID), part, subAssembly
FROM some_table
WHERE subAssembly = @.sub_assembly
GROUP BY part, subAssembly

INSERT INTO #temp_table2(t2_count, part, subAssembly)
SELECT COUNT(RecordID), part, subAssembly
FROM some other table
WHERE subAssembly = @.sub_assembly
GROUP BY part, subAssembly
END

UPDATE #temp_table1
SET t2_count =
(SELECT t2_count
FROM #temp_table2
WHERE part = #temp_table1.part)

SELECT *
FROM #temp_table1
WHERE t1_count <> t2_count
ORDER BY subAssembly, part

This seems to work fairly well. Thanks everyone for the input.

Friday, February 17, 2012

dynamic column name (using where)

Hello All,
I'm trying to us a parameter as column name like:
Select * From SM_Clients
where @.Selection like @.SelectionChar+'%'
This dos not work.
does someone have a solution :(
tks stroekDynamic SQL:
http://www.sommarskog.se/dynamic_sql.html|||I wanted a read but that website is blocked :mad:

Your request was denied because of its content categorization: "Personal Web Sites;Society and Lifestyles"|||yikes, george, yikes

then again, you do have a tendency to mess about when you should be working, and if others in your company are the same, then no wonder they've put controls on you...

:)|||then again, you do have a tendency to mess about when you should be working

:shocked: How very dare you!|||Sorry but I didn't find any simple solution for my problem.
Is there no one that can give me a simple solution?

Gr stroek|||DECLARE @.s varchar(150)

SET @.s = 'Select * From SM_Clients where '
SET @.s = @.s + @.Selection
SET @.s = @.s + ' like '''
SET @.s = @.s + @.SelectionChar
SET @.s = @.s + '%'''

EXEC (@.s)

?|||Yeah, my work wouldn't consider time spent here to be "messing about";)|||Mine either - an invaluable learning resource.
You only get out what you put in ;)|||i would've said skiving, but the rest of you lot would've thought i'd gone barmy|||I'm ahead of schedule on my projects and I consider this a constructive use of my time :)
I think I've learnt more from these forums than I have from all the courses I have been on combined.
Furthermore; learning by doing is also a much more effective way of learning imo.|||Unfortunately, I was being sarcastic. Spending a few hours of my work day giving support to others outside of the company (as I've done here in the past) would be severely frowned upon.

dynamic column name (using where)

Hello All,

I'm trying to us a parameter as column name like:

Select * From SM_Clients
where @.Selection like @.SelectionChar+'%'

This dos not work.

does someone have a solution

tks stroek

You cannot do that with the 'normal' way of executing T-SQL.

You can do it with dynamic SQL. You must first build the query as a string, and then execute the string. Somewhat like this:

DECLARE @.SQLString nvarchar(1000)

SET @.SQLString = 'SELECT * FROM SM_Clients WHERE ' +

@.Selection +

' LIKE ' @.SelectionChar + '%;'

Then just to verify that you have done the above correctly, you could test like this in a Query Window:

PRINT @.SQLString

Once you are satisifed that the query is properly constructed, you would execute it like this:

EXECUTE sp_executesql @.SQLString

However, if there is any chance that a user will be typing in values for @.Selection OR @.SelectionChar, your server 'could' be wide open for what is known as a SQL INJECTION ATTACK.

I highly recommend reviewing these articles:

Dynamic Search Conditions in T-SQL
http://www.sommarskog.se/dyn-search.html

Dynamic SQL - The Curse and Blessings of Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html
http://msdn2.microsoft.com/en-us/library/ms188332.aspx
http://msdn2.microsoft.com/en-us/library/ms175170.aspx

|||OK
tks I will try it.|||How can I use this in a stored procedure?|||

hi,

http://www.sommarskog.se/dynamic_sql.html

regards