Showing posts with label query. Show all posts
Showing posts with label query. Show all posts

Friday, March 30, 2012

Prediction Query in MS Association Rules

Hi!

I'm building a mining model wiht MS Association Rules. After processing this model, the result includes some rules(example):

E = Existing, C = Existing -> B = Existing
F = Existing -> E = Existing
C = Existing, B = Existing -> E = Existing
F = Existing -> B = Existing
B = Existing, A = Existing -> C = Existing
F = Existing, B = Existing -> E = Existing
F = Existing, E = Existing -> B = Existing
D = Existing -> A = Existing
C = Existing -> A = Existing
E = Existing, A = Existing -> B = Existing

I want to buid a query that has two or more items on the left of the rules, example: E = Existing, C = Existing -> B = Existing
->I want to buid a query to predict that: when a customer buy 'E' and 'C' then he likely buys 'B'


All the rules are used when you use AR for prediction. The first place to look is the prediction query builder. There is a button on the top to switch the mode from batch to singleton. With a singleton prediction you can manually specify the inputs for your query.

The prediction function you need to specify is something like "Predict(<my nested table name>, 5)". To build such a prediction in the query builder, select Prediction Function, then Predict, then type the name of your nested table, comma, then the number of recommendations you want into the parameters box.

To see the query select the SQL mode from the toolbar.

Let me know if this helps or if you were looking for some other type of answer

THanks

-Jamie

|||

Hi!

Thanks for interesting in my question!

My domain has two tables: Customer (Customer_ID, Name, ....) and Purchase (Customer_ID, Product_Name, Quantity,...)

Creating Mining Model:

Create Mining Model ProductPredict{

Customer_ID long key,

Purchase Table Predict {Product_Name text key}

}

So, when i buid a query such as:

Select Predict(Purchase, 3)

From ProductPredict Prediction Join

(Select 'A' As Product_Name

) as customer

On [ProductPredict].[Purchase].Product_Name = customer.Product_Name;

Result as all item in the right side of the rules contain 'A' in the left side.

But I want to buid a query that result as all item in the right side of the rules contain 'A' and 'B' in the left side.?

Summary: I want to buid a query that result as all item in the right side of the rules contain some items in the left side?

|||

You need a query such as

Select Predict(Purchase, 3)

From ProductPredict Prediction Join

(select

(Select 'A' As Product_Name UNION Select 'B' AS Product_Name)

as Products

) as customer

On [ProductPredict].[Purchase].Product_Name = customer.Product_Name;

This will cause rules with A and B to fire. You may still get predictions based on A alone and B alone, though, depending on their probability and lift.

|||

Hi!

Thank you very much! That's interesting, but when i run that query, it has error, so the correct query is:

Select PredictAssociation(Purchase, 3)

From ProductPredict Prediction Join

(

select

( Select 'A' As Product_Name

UNION

Select 'B' AS Product_Name

)as Products

) as customer

On [ProductPredict].[Purchase].Product_Name = [customer].[Products].Product_Name

|||Predict is a polymorphic function - DMX maps it to the appropriate function based on the model that's being queried. In this case, it maps to PredictAssociation so you should get the same results either way. What errors did you see with the earlier query?

Prediction Query for a "weighted" clustering model

I have a question about writing a prediction query against a clustering model that has the same column added more than once.

Per Jamie, I can accomplish some crude weighting by adding a column to my model multiple times. See this post for an explnation... Now that I have that worked out, I was wondering how my DM query would look? If I have Input_A1, Input_A2 , & Input_A3 all being source from the same column in my structure do I have to reference all three when writing my prediction query?

to be most theoretically accurate, yes, however, I would check to see how the results change for your particular model as you change inputs. If you don't have any missing data, it may not make a significant difference.sql

Prediction Join to MDX with nested table

If your prediction join is to a SQL datasource, you can easily write a SQL query which returns a nested table like:

SELECT
Predict([Subcategories],2) as [Subcategories]
FROM
[SubcategoryAssociations]
NATURAL PREDICTION JOIN
(SELECT
(SELECT 'Road Bikes' AS Subcategory
UNION SELECT 'Jerseys' AS Subcategory
) AS Subcategories
) AS t

What about if your datasource is a cube? Is there some special MDX syntax similar to the SQL syntax above? Or do you have to utilize the SHAPE/APPEND syntax as follows?

SELECT t.*, $Cluster as ClusterName
FROM [MyModel]
PREDICTION JOIN
SHAPE {
select [Measures].[My Measure] on 0,
[My Dimension].[My Attribute].[My Attribute].Members on 1
from MyCube
}
APPEND (
{
select [Measures].[Another Measure] on 0,
NON EMPTY [My Dimension].[My Attribute].[My Attribute].Members
*[Product].[Product].[Product].Members on 1
from MyCube
}
RELATE [[My Dimension]].[My Attribute]].[My Attribute]].[MEMBER_CAPTION]]]
TO [[My Dimension]].[My Attribute]].[My Attribute]].[MEMBER_CAPTION]]]
)
AS [My Nested Table] AS t
ON [MyModel].[Product].[Product] = t.[My Nested Table].[[Product]].[Product]].[Product]].[MEMBER_CAPTION]]]

Typically, for building models on top of cubes, it is much easier to use the tools (BI Dev Studio). This way you can define your model directly on top of the cube and lots of optimizations occur. With such models, you can even use the MDXPredict function to get prediction results inside MDX queries over the source cube.

The DMX SELECT statement supports as input rowset-returning Analysis Services statements (MDX or DMX). That means that dataset-returning statements are not supported. But many MDX queries can be flattened. Have you tried something like SELECT FLATTENED in the MDX query?

|||

Bogdan-

Thanks for the reply. Yes, BIDS worked great for building the model. I've got it trained. Now I want to do a prediction based upon data from a cube. From what I can tell, you can't do prediction queries off a cube using BIDS because it only lets you predict off a relational table source. Right?

I've been researching the MDX function "Predict" which you mentioned. But I'm having terrible trouble finding example queries using that function...

Here's what I'm looking for... we've built a clustering model to cluster our stores. Some of the attributes are just Store dimension attributes... some are from a nested table (stats about the sales volume from each product category). We trained the model with all the stores. Now we want to extract the cluster name for each store and save that to a table. So is there a straight MDX query using the Predict MDX function which will get me the cluster name for every store? I was having trouble seeing how the Predict MDX function was able to know how to do a prediction join to the Store dimension.

As a side note, we could almost do a natural prediction join back to (select * from Model.CASES) except that we don't want the Store Key to influence the clustering model so we didn't add that as an input to the model. (And marking Store Key as Ignore excludes it from the Model.CASES resultset.)

By the way, we're only talking about a couple hundred rows, so the performance of the SHAPE/APPEND syntax below is fine for my purposes... just seeing if there's a more elegant way to do it.

Thanks!

|||

Oh... and to answer your other question about trying "SELECT FLATTENED"...

It's my understanding that "SELECT FLATTENED" is DMX. I'm not sure how to write an MDX statement that starts with "SELECT FLATTENED". And I'm struggling to see how using the DMX "SELECT FLATTENED" would help me. The output of DMX prediction query I used in the examples at the beginning of the thread work fine. I suppose I could flatten the output, but that wouldn't help me much. It's the input to the prediction join that I'm concerned with.

Or did you mean that you can use an MDX query which is written to be flat and use that as input to a prediction query which expects nested tables? I just tried that but may not have been using the right syntax cause I couldn't get it to work. Suggestions?

|||

You kind of need to do it brute force -we use the flattening semantics of MDX when executing the query, so you have to reshape using SHAPE.

There is a little trick to help you out in building the queries. You can use DMX to examine the flattened structure of the MDX query. Just issue a query like this:

SELECT t.* FROM AnyModel NATURAL PREDICTION JOIN <My MDX Query> AS t

then you will be able to see how the DMX processor sees your MDX results.

|||

Jamie-

That trick is helpful for seeing how it refers to the results of an MDX query.

But how do I take a flat MDX query and shape it so it can be consumed by a prediction join which expects a nested table. See the MDX example at the top of this post. Is that the only way (tying two separate MDX queries together with SHAPE/APPEND)?

|||

Yes your original SHAPE/APPEND would be the way to go.

The implementation of SHAPE in the AS engine will cause the MDX query results to be automatically returned in a flattened manner without requiring any explicit flattening syntax in the query itself (in fact, there is no such syntax - flattening is requested as either a command property in XMLA or by requesting a rowset interface in OLE DB)..

Predict Probability in Decision Trees

Hello,

I installed the bike buyer example and i am learning the DMX language. Now i wrote the following query (using MS decision trees):

SELECT
T.[Last Name],
[Bike Buyer],
PredictProbability(Predict([Bike Buyer])) AS [Probability]
From
[v Target Mail]
PREDICTION JOIN
OPENQUERY
(....... And so on..)

Now the result is surprising to me. In the resulttabel all the probabilities are equal.

Bike Buyer Probability
1 0.99994590500919611
0 0.99994590500919611
0 0.99994590500919611
0 0.99994590500919611
0 0.99994590500919611
1 0.99994590500919611

and so on.

Now i am wondering what predictProbability means. I thought that PredictProbability meant the probability that the prediction is correct. Now all the probabilities are the same and the input is different. Can somebody tell me what PredictProbability means or am I using it wrong?

Thanx in advance,

Joris Valkonet

This is an interesting query - I would write it as "PredictProbability([Bike Buyer])" however, the syntax is semantically the same. Another thing to try is PredictNodeId([Bike Buyer]) to see exactly the node used for the prediction and then check that node to see the distribution.

It's possible that PredictProbability(Predict([Bike Buyer])) is exposing a bug and you should use the simpler syntax above.

HTH

-Jamie

|||

Thank you for your reply.
I modified my query to PredictProbability([bike buyer]) and this made no difference. The result is the same.

Then I checked the PredictNodeId([Bike Buyer]) and this is the result:

FirstName

LastName

bike buyer

Expression

Abby

Malhotra

1

000000003

Abby

Prasad

0

000000003

Abby

Rodriguez

0

000000003

Abby

Srini

0

000000003

Abigail

Brown

0

000000003

Abigail

Bryant

1

000000003

Abigail

Davis

0

000000003

Abigail

Flores

0

000000003

Now the nodeId is the rootnode of the tree and the probability of the root node is indeed the value 0.99994590500919611. So if I understand this correct, the rootnode of the decision tree is used for all probability predictions. Is this correct?

(Maybe my query is incorrect and therefore the whole DMX query below)

SELECT
t.[FirstName],
t.[LastName],
[bike buyer],
PredictNodeId([bike buyer]) AS [Predicted NodeId]
From
[v Target Mail]
PREDICTION JOIN
OPENQUERY([Adventure Works DW],
'SELECT
[Gender],
[FirstName],
[MiddleName],
[LastName],
[BirthDate],
[MaritalStatus],
[EmailAddress],
[YearlyIncome],
[TotalChildren],
[NumberChildrenAtHome],
[HouseOwnerFlag],
[NumberCarsOwned],
[AddressLine1],
[AddressLine2],
[Phone]
FROM
[dbo].[ProspectiveBuyer]
') AS t
ON
[v Target Mail].[First Name] = t.[FirstName] AND
[v Target Mail].[Middle Name] = t.[MiddleName] AND
[v Target Mail].[Last Name] = t.[LastName] AND
[v Target Mail].[Birth Date] = t.[BirthDate] AND
[v Target Mail].[Marital Status] = t.[MaritalStatus] AND
[v Target Mail].[Gender] = t.[Gender] AND
[v Target Mail].[Email Address] = t.[EmailAddress] AND
[v Target Mail].[Yearly Income] = t.[YearlyIncome] AND
[v Target Mail].[Total Children] = t.[TotalChildren] AND
[v Target Mail].[Number Children At Home] = t.[NumberChildrenAtHome] AND
[v Target Mail].[House Owner Flag] = t.[HouseOwnerFlag] AND
[v Target Mail].[Number Cars Owned] = t.[NumberCarsOwned] AND
[v Target Mail].[Address Line1] = t.[AddressLine1] AND
[v Target Mail].[Address Line2] = t.[AddressLine2] AND
[v Target Mail].[Phone] = t.[Phone]

|||

Something wierd is happening here - Predict([Bike Buyer]), which is equivalent to saying just [Bike Buyer], returns the highest probability value for the prediction. That means that whatever node the tree goes to, it will return the highest probability state for that node. I can't see how you are getting different prediction results from the same node.

However, something else struck me as odd. The predict probability of 0.999... doesn't seem right. In fact the only time I have ever seen anything like this is when all the data at the node was missing, and the actual non-missing values only recieved the bayesian prior. By default prediction never returns missing (if you are asking for a value, it assumes you want an actual value). If you do a Predict([Bike Buyer], INCLUDE_MISSING) you will get predictions that include the missing value. If there were no observations in the training data of the states 1 and 0 then they would only have their prior probabilities which would be equal and then the choice of a state would be arbitrary.

What confuses me is that PredictProbabililty should behave the same way.

Can you do a SELECT FLATTENED NODE_DISTRIBUTION FROM [v Target Mail].CONTENT and post the results?

Thanks

|||

Thanks for your analysis. I tried to post the whole result of the query "SELECT FLATTENED NODE_DISTRIBUTION FROM [v Target Mail].CONTENT ", but then an unknown error occurs. I think because the resulttable is to big. So... i will give the first top rows of the result.

Thanks....

ATTRIBUTE_NAME

ATTRIBUTE_VALUE

SUPPORT

PROBABILITY

VARIANCE

.VALUETYPE

Bike Buyer

Missing

0

0.000432432

0

1

Bike Buyer

0.494048907

18484

0.999567568

0.249964584

3

Birth Date

5.45E-06

0

0

16892175

7

Birth Date

0.080783333

0

0

0

8

Birth Date

22673.85945

0

0

16892175

9

Date First Purchase

-0.000941648

0

0

76769.40019

7

Date First Purchase

3242.190525

0

0

0

8

Date First Purchase

37852.25763

0

0

76769.40019

9

Number Cars Owned

-0.079859089

0

0

1.295870198

7

Number Cars Owned

301.5988757

0

0

0

8

Number Cars Owned

1.502705042

0

0

1.295870198

9

Number Children At Home

-0.015796323

0

0

2.318367003

7

Number Children At Home

9.530872281

0

0

0

8

Number Children At Home

1.004057563

0

0

2.318367003

9

Total Children

-0.003992138

0

0

2.599718694

7

Total Children

2.60E+01

0

0

0

8

Total Children

1.844351872

0

0

2.599718694

9

Yearly Income

2.02E-06

0

0

1042319181

7

Yearly Income

116.9361238

0

0

0

8

Yearly Income

57305.77797

0

0

1042319181

9

36.04143269

0

0

0.166743652

11

Bike Buyer

Missing

0

0.000205761

0.00E+00

1

Bike Buyer

1

4858

0.999794239

0

3

1

0

0

5.14E-07

11

Bike Buyer

Missing

0

0.000536481

0

1

Bike Buyer

0.507518797

1862

0.999463519

0.249943468

3

Date First Purchase

-0.010374704

0

0

1160.841795

7

Date First Purchase

679.5475673

0

0

0

8

Date First Purchase

37822.50644

0

0

1160.841795

9

Geography Key

0.000170418

0

0

36083.71548

7

Geography Key

1.990727272

0

0

0

8

Geography Key

236.4581096

0

0

36083.71548

9

Number Cars Owned

-0.072103841

0

0

1.254812169

7

Number Cars Owned

2.37E+01

0

0

0

8

Number Cars Owned

1.523630505

0

0

1.254812169

9

Yearly Income

1.29E-06

0

0

1131929217

7

Yearly Income

7.102048444

0

0

0

8

Yearly Income

60633.72718

0

0

1131929217

9

392.8960717

0

0

0.113511443

11

Bike Buyer

Missing

0

0.000123274

0

1

Bike Buyer

0.256103576

8110

0.999876726

0.190514534

3

|||

I see the problem - you have Bike Buyer modeled as continuous where it should be discrete. PredictProbability was giving you the probability that BikeBuyer exists in this case. To get the confidence of the prediction for continuous predictions, you want to use PredictStdev.

Essentially what was happening was that the model was creating a linear regression to predict bike buyer rather than a classification model. Changing the content type to "Discrete" will fix your issue. If you are creating the model from the source table (rather than a cube), you can click on "Detect" on the data types page of the wizard and it will automatically determing that Bike Buyer should be discrete.

Predict % completed

I'm doing a web search page on a db that has about 1 million records, and
the query is quite complicated. I would like to provide the user some
feedback will the db server is performing the search.
What is the best way to do this? any examples or tutorials?
The only way I can think of is doing 10000 records at a time and add 1% to a
counter.
There must be a better way.
Any input is greatly appreciated,
AaronWhat about firing first the Query to the Optimizer and sshowing the
execution plan for it, you might see there how long (CPU cycles) it will
last and how many rows are estimated (Requirement of maintained statistics
:-)
See for this one:
USE NORTHWIND
GO
SET SHOWPLAN_ALL ON
GO
Select CompanyName from Customers where CompanyName LIKE '%A%'
GO
SET SHOWPLAN_ALL OFF
GO
(Result gonna spread over the page, best thing you execute in on your own.)
HTH, Jens Smeyer
http://www.sqlserver2005.de
--
"Aaron" <kuya789@.yahoo.com> schrieb im Newsbeitrag
news:O503JyYQFHA.204@.TK2MSFTNGP15.phx.gbl...
> I'm doing a web search page on a db that has about 1 million records, and
> the query is quite complicated. I would like to provide the user some
> feedback will the db server is performing the search.
> What is the best way to do this? any examples or tutorials?
> The only way I can think of is doing 10000 records at a time and add 1% to
> a counter.
> There must be a better way.
> Any input is greatly appreciated,
> Aaron
>
>|||what's the Optimizer ? is that an addon to sql server?
i don't think i have that
"Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> wrote in
message news:eu5VT4YQFHA.1392@.TK2MSFTNGP10.phx.gbl...
> What about firing first the Query to the Optimizer and sshowing the
> execution plan for it, you might see there how long (CPU cycles) it will
> last and how many rows are estimated (Requirement of maintained statistics
> :-)
> See for this one:
>
> USE NORTHWIND
> GO
> SET SHOWPLAN_ALL ON
> GO
> Select CompanyName from Customers where CompanyName LIKE '%A%'
> GO
> SET SHOWPLAN_ALL OFF
> GO
> (Result gonna spread over the page, best thing you execute in on your
> own.)
> HTH, Jens Smeyer
>
> --
> http://www.sqlserver2005.de
> --
> "Aaron" <kuya789@.yahoo.com> schrieb im Newsbeitrag
> news:O503JyYQFHA.204@.TK2MSFTNGP15.phx.gbl...
>|||On Fri, 15 Apr 2005 16:06:44 -0700, Aaron wrote:

>what's the Optimizer ? is that an addon to sql server?
>i don't think i have that
(snip)
Hi Aaron,
The optimizer is an integral part of SQL Server. It's the part of the
server that reviews your query and determines the execution plan (the
order and methods in which data will be fetched and combined to satisfy
your request).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

Wednesday, March 28, 2012

Precedence of MAX and WHERE

Hi
I'd like to create a query which returns the MAX of a group of dates so long
as the number is less than a given date. For example :
SELECT MAX(date), username
FROM mydatatable
WHERE date < '01/01/2005'
GROUP BY username
Will this do what I expect and return the username and date which is the
most recent before 01/01/2005 ?
Thanks
AndrewHi,
Your query looks good.
Thanks
Hari
SQL Server MVP
"Andrew Webb" <andrew.webb@.eme-med.co.uk> wrote in message
news:OTlNYqfuFHA.1572@.TK2MSFTNGP10.phx.gbl...
> Hi
> I'd like to create a query which returns the MAX of a group of dates so
> long as the number is less than a given date. For example :
>
> SELECT MAX(date), username
> FROM mydatatable
> WHERE date < '01/01/2005'
> GROUP BY username
>
> Will this do what I expect and return the username and date which is the
> most recent before 01/01/2005 ?
> Thanks
> Andrew
>|||You could compare these queries and see which one yields the results you
want. Word problems are tough to solve, usually better to provide specs as
described in http://www.aspfaq.com/5006 . Also, "date" is a really bad name
for a column. Not only is it a reserved word, it is also very tough to
decipher it... date of WHAT? Finally, do not use m/d/y or d/m/y date
formats when hard-coding date strings. The safest approach here is to use
YYYYMMDD format, then this can't be by software or humans.
CREATE TABLE dbo.myDataTable
(
username VARCHAR(32),
eventDate SMALLDATETIME
)
GO
SET NOCOUNT ON
INSERT myDataTable SELECT 'bob','20040101'
INSERT myDataTable SELECT 'bob','20050201'
INSERT myDataTable SELECT 'frank','20040101'
INSERT myDataTable SELECT 'frank','20040725'
GO
SELECT username, MAX(eventDate)
FROM dbo.myDataTable
WHERE eventDate < '20050101'
GROUP BY username
SELECT username, MAX(eventDate)
FROM dbo.myDataTable
GROUP BY username
HAVING MAX(eventDate) < '20050101'
GO
DROP TABLE dbo.myDataTable
GO
"Andrew Webb" <andrew.webb@.eme-med.co.uk> wrote in message
news:OTlNYqfuFHA.1572@.TK2MSFTNGP10.phx.gbl...
> Hi
> I'd like to create a query which returns the MAX of a group of dates so
> long as the number is less than a given date. For example :
>
> SELECT MAX(date), username
> FROM mydatatable
> WHERE date < '01/01/2005'
> GROUP BY username
>
> Will this do what I expect and return the username and date which is the
> most recent before 01/01/2005 ?
> Thanks
> Andrew
>|||Andrew,

> Will this do what I expect and return the username and date which is the
> most recent before 01/01/2005 ?
It is correct, but it could be more than one. It will select each username
and the max date for those username with date values less than '20050101'. I
f
a username does not have date values in this range then it will not appear i
n
the result.
AMB
"Andrew Webb" wrote:

> Hi
> I'd like to create a query which returns the MAX of a group of dates so lo
ng
> as the number is less than a given date. For example :
>
> SELECT MAX(date), username
> FROM mydatatable
> WHERE date < '01/01/2005'
> GROUP BY username
>
> Will this do what I expect and return the username and date which is the
> most recent before 01/01/2005 ?
> Thanks
> Andrew
>
>

Monday, March 26, 2012

Prblem to store XML result into an output veriable on SQL 2000

Hi,

I want to store the result of the query

SELECT * FROM Customer FOR XML AUTO,ELEMENTS

Into an output veriable. How will I do this in SQL Server 2000?

I've tried this in simple way like

declare @.x varchar(1000)

set @.x = (select * from customer for xml auto,elements)

select @.x

This is perfectly working in SQL 2005 but throwing error in 2000

also in I've tried this using cursor, TempTable on SQL Server 2000.

Please help me.

You can't able to do this in SQL Server 2000. In SQL Server 2000 we don't have XML datatype. It is introduced from SQL Server 2005 only.

The only possible solution is manullay concatinating the values, but it is very expensive and there is char length limitation (8000) may cause truncation of your data.

|||

Thank you very much.

Prblem to store XML result into an output veriable on SQL 2000

Hi,

I want to store the result of the query

SELECT * FROM Customer FOR XML AUTO,ELEMENTS

Into an output veriable. How will I do this in SQL Server 2000?

I've tried this in simple way like

declare @.x varchar(1000)

set @.x = (select * from customer for xml auto,elements)

select @.x

This is perfectly working in SQL 2005 but throwing error in 2000

also in I've tried this using cursor, TempTable on SQL Server 2000.

Please help me.

You can't able to do this in SQL Server 2000. In SQL Server 2000 we don't have XML datatype. It is introduced from SQL Server 2005 only.

The only possible solution is manullay concatinating the values, but it is very expensive and there is char length limitation (8000) may cause truncation of your data.

|||

Thank you very much.

PRB: Single Query to build database and schema.

PRB: Single Query to build database and schema.
Please help, I'm quite frustrated with a problem.
I want to issue a SINGLE query to completely build my database (IF IT
DOESN'T ALREADY EXIST), "use" it, build an entire schema of tables, stored
procedures, triggers, and even populate tables with data.
Envision a typical ASP/ADO using program calling the query as such:
========================================
====
<%@. Language=JavaScript %>
<%
var Connection_Temp;
var ResultSet_Temp;
var csMY_QRY, csOPTION, csMY_DB_NAME;
csMY_QRY = "~~~~~~~ What ever ~~~~~~~~";
csMY_QRY = csMY_QRY.replace("<%=DB%>", csMY_DB_NAME);
csMY_QRY = csMY_QRY.replace("<%=OPTION%>", csOPTION);
.
.
.
Connection_Temp = Server.CreateObject("ADODB.Connection");
Connection_Temp.ConnectionTimeout = 45;
Connection_Temp.Open("DRIVER={SQL
Server};SERVER=My_SVR;UID=MY_UID;PWD=MY_
PWD");
ResultSet_Temp = Connection_Temp.Execute(csMY_QRY);
%>
<html>
</html>
========================================
====
There are several "issues" which are making this very difficult to solve:
1) "CREATE DATABASE" and "USE" do not accept variables.
2) "USE" can not be used in a stored procedure or trigger.
3) sp_executesql only accepts NVARCHAR, NTEXT.
4) NVARCAHR can only be declared up to 4000 bytes.
5) NTEXT data types can only be declared as a parameter, not with "DECLARE".
6) NTEXT defined as parameters can not be assigned with SELECT statements.
7) A typical schema will surely require more than 4000 characters to be
represented
as a string.
8) Even tables with NTEXT can not be fetched into a NTEXT variable such as
this:
create table TEST(JUNK ntext, NAME nvarchar(50) primary key)
insert into TEST(NAME, JUNK) values('ME', 'JUNK')
create procedure TRY(ntext @.csTRY) as
begin
select @.csTRY = JUNK from TEST where NAME = 'ME' -- This fails...
-- Can't assign the NTEXT.
end
9) "GO" can not be used in a stored procedure"
10) "GO" can not be used in a single query passed to ADO as in the above
example.
Put another way, imagine setting csMY_QRY as such:
csMY_QRY = "go \r\n exec sp_help \r\n go exec sp_help";
This will not work in the sample above. SQL Server will say
[Microsoft][ODBC SQL Server Driver][SQL Server]The object 'go' does not
exist in database 'master'.
11) Building the schema MUST be done with transaction handling, so that if
one "CREATE TABLE" fails, they all roll back.
12) THIS HAS TO BE A QUERY.
I am convinced that this can be done. It may require a LOT of sp_executesql
calls, but I feel it can be done.
Any ideas and/or help would be much appreciated.
If nothing else, I would very much like for Microsoft to publish the final
outcome of this PRB as a full blown MSDN article to help everyone. I've
noticed here in the discussion groups that many people are trying to
accomplish this, but are typically getting hung on "CREATE DATABASE" or "USE
"
limitations.I have an update already.
I believe that this WHOLE PRB can be solved if we can do one of the followin
g:
Use some sort of SP to select from a temporary table that has an NTEXT, to
fetch a list of queries, where each query is then executed by something like
sp_executesql, where if ANY failure occurs, the caller will know it, and the
n
be able to roll that query back, and any previous query.
The key is to somehow have an NTEXT variable to get the query, and then pass
it to a sp_executesql as such:
~~~~ somehow DECALRE ~~~~~ @.QRY ntext
select * from QRY_TABLE_WITH_NTEXT_QUERIES
~~~ CURSOR: for each row returned, select into @.QRY ~~~
exec sp_executesql @.QRY
~~~ fetch next - do loop|||Yet another update.
I'm almost there.
The NTEXT is the KEY issue.
If ONLY I could programmatically create and populate an NTEXT variable, WITH
data from a "select * from TABLE_WITH_NTEXT_COLUMN", then I'm sure this PRB
would be solved.
Here is a sample QRY I have made so far. It fails on the FETCH NEXT, where
the error is "[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot fetch
into text, ntext, and image variables. "
===============================
begin transaction
set XACT_ABORT on
set NOCOUNT on
declare @.csQRY nvarchar(4000)
declare @.CrLf nvarchar(5)
declare @.QQ nvarchar(5)
select @.CrLf = char(13) + char(10)
select @.QQ = char(39) + char(39)
select @.csQRY = 'create table QTs([NAME] nvarchar(50), [QRY] ntext)'
exec sp_executesql @.csQRY
select @.csQRY = 'insert into QTs([NAME], [QRY]) values(' + char(39) + 'QRY'
+ char(39) + ', ' + char(39) + 'create database MY_DB' + char(39) + ')'
exec sp_executesql @.csQRY
select @.csQRY = 'insert into QTs([NAME], [QRY]) values(' + char(39) + 'QRY'
+ char(39) + ', ' + char(39) + 'create table MY_DB.dbo.[FUN]([NAME]
nvarchar(50))' + char(39) + ')'
exec sp_executesql @.csQRY
select @.csQRY =
'create procedure DO_QRY(@.QRY ntext = ' + @.QQ + ') as' + @.CrLf +
'begin' + @.CrLf +
' begin transaction' + @.CrLf +
' declare TRY_CURSOR cursor for select [QRY] from QTs' + @.CrLf +
' open TRY_CURSOR' + @.CrLf +
' fetch next from TRY_CURSOR into @.QRY' + @.CrLf +
' while @.@.FETCH_STATUS = 0' + @.CrLf +
' begin' + @.CrLf +
' exec sp_executesql @.QRY' + @.CrLf +
' if (@.@.ERROR <> 0)' + @.CrLf +
' begin' + @.CrLf +
' rollback transaction' + @.CrLf +
' return' + @.CrLf +
' end' + @.CrLf +
' fetch next from TRY_CURSOR into @.QRY' + @.CrLf +
' end' + @.CrLf +
' commit transaction' + @.CrLf +
'end' + @.CrLf
exec sp_executesql @.csQRY
select @.csQRY = 'exec DO_QRY'
exec sp_executesql @.csQRY
commit transaction|||Back to being dead in the water.
I replaced the NTEXT with NVARCHAR(3000), and tried the same login. It
failed on the create database. Now what?
begin transaction
set XACT_ABORT on
set NOCOUNT on
declare @.csQRY nvarchar(4000)
declare @.CrLf nvarchar(5)
declare @.QQ nvarchar(5)
select @.CrLf = char(13) + char(10)
select @.QQ = char(39) + char(39)
select @.csQRY = 'create table QTs([NAME] nvarchar(50), [QRY] nvarchar(3900))'
exec sp_executesql @.csQRY
select @.csQRY = 'insert into QTs([NAME], [QRY]) values(' + char(39) + 'QRY'
+ char(39) + ', ' + char(39) + 'create database MY_DB' + char(39) + ')'
exec sp_executesql @.csQRY
select @.csQRY = 'insert into QTs([NAME], [QRY]) values(' + char(39) + 'QRY'
+ char(39) + ', ' + char(39) + 'create table MY_DB.dbo.[FUN]([NAME]
nvarchar(50))' + char(39) + ')'
exec sp_executesql @.csQRY
select @.csQRY =
'create procedure DO_QRY as' + @.CrLf +
'begin' + @.CrLf +
' begin transaction' + @.CrLf +
' declare @.QRY nvarchar(4000)' + @.CrLf +
' declare TRY_CURSOR cursor for select [QRY] from QTs' + @.CrLf +
' open TRY_CURSOR' + @.CrLf +
' fetch next from TRY_CURSOR into @.QRY' + @.CrLf +
' while @.@.FETCH_STATUS = 0' + @.CrLf +
' begin' + @.CrLf +
' exec sp_executesql @.QRY' + @.CrLf +
' if (@.@.ERROR <> 0)' + @.CrLf +
' begin' + @.CrLf +
' rollback transaction' + @.CrLf +
' return' + @.CrLf +
' end' + @.CrLf +
' fetch next from TRY_CURSOR into @.QRY' + @.CrLf +
' end' + @.CrLf +
' commit transaction' + @.CrLf +
'end' + @.CrLf
exec sp_executesql @.csQRY
select @.csQRY = 'exec DO_QRY'
exec sp_executesql @.csQRY
commit transaction
=================================
In QA, the return is as such:
==================================
Server: Msg 266, Level 16, State 2, Procedure DO_QRY, Line 14
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK
TRANSACTION statement is missing. Previous count = 1, current count = 0.
Server: Msg 266, Level 16, State 1, Line 1
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK
TRANSACTION statement is missing. Previous count = 1, current count = 0.
Server: Msg 226, Level 16, State 1, Line 1
CREATE DATABASE statement not allowed within multi-statement transaction.
Server: Msg 3902, Level 16, State 1, Line 38
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.
===============================
Any ideas?|||SOLVED!!!!!!
The CREATE DATABASE can be issued. I just had to commit any TX that may have
existed before the CREATE DATABASE for it to work. Then I could resume
another "BEGIN TRANSACTION" and continue on. I also needed to use a
WHILE-LOOP to be sure to commit all transactions that may be created by the
various CREATE TABLEs I would execute. But I did get it to work. Final sampl
e
below:
============ This should be published into MSDN, no fooling!
=======================================
begin transaction
set XACT_ABORT on
set NOCOUNT on
declare @.csQRY nvarchar(4000)
declare @.CrLf nvarchar(5)
declare @.QQ nvarchar(5)
select @.CrLf = char(13) + char(10)
select @.QQ = char(39) + char(39)
select @.csQRY = 'create database OptiDoc_3X'
commit transaction
exec sp_executesql @.csQRY
begin transaction
select @.csQRY = 'create table QTs([NAME] nvarchar(50), [QRY] nvarchar(3900))'
exec sp_executesql @.csQRY
--select @.csQRY = 'insert into QTs([NAME], [QRY]) values(' + char(39) +
'QRY' + char(39) + ', ' + char(39) + 'create database MY_DB' + char(39) + ')
'
select @.csQRY = 'insert into QTs([NAME], [QRY]) values(' + char(39) + 'QRY'
+ char(39) + ', ' + char(39) + 'create table MY_DB..MY_TBL([NAME]
nvarchar(50))' + char(39) + ')'
exec sp_executesql @.csQRY
begin transaction
select @.csQRY =
'create procedure DO_QRY as' + @.CrLf +
'begin' + @.CrLf +
' begin transaction' + @.CrLf +
' declare @.QRY nvarchar(4000)' + @.CrLf +
' declare TRY_CURSOR cursor for select [QRY] from QTs' + @.CrLf +
' open TRY_CURSOR' + @.CrLf +
' fetch next from TRY_CURSOR into @.QRY' + @.CrLf +
' while @.@.FETCH_STATUS = 0' + @.CrLf +
' begin' + @.CrLf +
' select @.QRY' + @.CrLf +
' exec sp_executesql @.QRY' + @.CrLf +
' if (@.@.ERROR <> 0)' + @.CrLf +
' begin' + @.CrLf +
' select ' + @.QQ + '-- Error!!!' + @.QQ + @.CrLf +
' rollback transaction' + @.CrLf +
' return' + @.CrLf +
' end' + @.CrLf +
' fetch next from TRY_CURSOR into @.QRY' + @.CrLf +
' end' + @.CrLf +
' close TRY_CURSOR' + @.CrLf +
' deallocate TRY_CURSOR' + @.CrLf +
' commit transaction' + @.CrLf +
'end' + @.CrLf
exec sp_executesql @.csQRY
select @.csQRY = 'exec DO_QRY'
exec sp_executesql @.csQRY
select @.csQRY = 'drop procedure DO_QRY'
exec sp_executesql @.csQRY
select @.csQRY = 'drop table QTs'
exec sp_executesql @.csQRY
while @.@.TRANCOUNT > 0
begin
commit transaction
end

PP: XML Variable DataLength returns as 5

Hi Folks
Here is what I found, when I execute this query
set nocount on
Declare @.xmlSourceDestinationAttributes XML
Select @.xmlSourceDestinationAttributes = ''
--Select @.xmlSourceDestinationAttributes
select Datalength(@.xmlSourceDestinationAttributes)
--
5
Question
======= How come I get a value of 5 even tough I passed nothing.Try using
select CAST(@.xmlSourceDestinationAttributes AS VARBINARY(MAX))
and you will see the BOM that is at the beginning of the xml document.
Dan
> set nocount on
> Declare @.xmlSourceDestinationAttributes XML
> Select @.xmlSourceDestinationAttributes = ''
> --Select @.xmlSourceDestinationAttributes
> select Datalength(@.xmlSourceDestinationAttributes

Friday, March 23, 2012

power and ^

Hi,
I am try to replicate an access query that contains the following syntax
173.3^2 which in access = 17.33
the access help describes the ^ operator as Used to raise a number to the power of an exponent.

I tried to replicate this with the power function
power(173.3,2) = 33043.29
so i guess power is not the right way to replicate this.
can any tell me how i can replicate this correctly ?Access also return the same result as power() function. What is the logic behind here.|||It turns out that it was the way that access priorities it's mathematic operations,
it did a ^ before a division.
so 173.3/10^2 results in 173.3 /100.
i had written the t-sql code as power(173.3/10,2) when it should have been 173.3/ power(10,2)
so my own fault did not read the code right.sql

Posts that are posted does not appear in the posted list

Hi,
I have posted a query in this newsgroup 5 hours ago and the post has not yet appeared in the list of posts.
Any suggestions?
Thanks
I see several posts from you about deploying MSDE. My suggestion? Use a
real newsreader.
http://www.aspfaq.com/5007
http://www.aspfaq.com/
(Reverse address to reply.)
"sm" <sm@.discussions.microsoft.com> wrote in message
news:2C8A1929-4674-43ED-AD1F-8C46065F0ACB@.microsoft.com...
> Hi,
> I have posted a query in this newsgroup 5 hours ago and the post has not
yet appeared in the list of posts.
> Any suggestions?
> Thanks

Wednesday, March 21, 2012

Posting Multi-Value Parameter

Hi,

I tried Posting the values for a multi value parameter from my application to the reporting services, But the query string is not being hidden. Why is that ? since the the data is sent through the browser address bar, I am not able to send values more than the allowed lenght. Is there any work around ?

Thanks In Advance

Regards

Raja Annamalai S

Hi Raja-

Yes, you will be limited to the URL length restriction. If possible I might suggest using the Web Service to render reports rather than crafting the URL. This will not be limited by URL length.

Otherwise you can perform a POST instead of a GET on the http call. The post will send the parameter values in the body rather than appended to the URL. However, all parameters need to be in the body if you perform a POST.

Thanks, Jon

Posting a query?

Its very difficulty to find the link to post query...

In this forums site...pls tell me the location...where can I find the link to post a query...

Gotohttp://forums.asp.net/ click on a particular category and above the main grid you'll find a 'Write a New Post' button.

PostgreSQL to XML!

Hi, everybody!.
I=B4m very new using XML and PostgreSQL so I have some questions:
1) I would like to know if there is a way to convert a query SQL and
return the request in an XML document. I=B4m using PostgreSQL to
devoloping the database. How I could to do this?
2) What is the principal advantage to do this?
3) Somebody know if there is somebody working with this? (study case,
articles, papers, journals, universities).
Thanks.
JoanaJoana G. Malaverri wrote:
> Hi, everybody!.
> Im very new using XML and PostgreSQL so I have some questions:
> 1) I would like to know if there is a way to convert a query SQL and
> return the request in an XML document. Im using PostgreSQL to
> devoloping the database. How I could to do this?
See the documentation
<URL:http://www.postgresql.org/docs/8.2/...tatype-xml.html>.
Note that this newsgroup is about XML in Microsoft SQL Server, if you
need further help with Postgres then a newsgroup, forum or mailing list
dedicated to Postgres seems a better place to look for.
Martin Honnen -- MVP XML
http://JavaScript.FAQTs.com/

PostgreSQL

In a Union query, I have initialized one new column for sorting purpose. For
ex :
select ORD = 1 , t1.a, t1.b from table1 as t1
Union
select ORD = 0, t2.a, t2.b from table2 as t2
order by ORD.
ORD column is not present in thw table. It is used here for putting all the
rows of the second query before the rows of first query. The query is
getting executed in Windows which uses SQL 2000 server. But in Linux(RedHat)
when we are using PostgresSQL database, the same query gives error - "Unable
to parse 'ORD' ".
Is this thing not supported in PostgresSQL? If this is not supported then
what is the method of acheiving the same result?
Thanks,
Venkat
Try:
select 1 AS ORD , t1.a, t1.b from table1 as t1
Union
select 0, t2.a, t2.b from table2 as t2
order by ORD
column_alias = expression is T-SQL specific and not ANSI-SQL standard, as
opposed to expression AS column_alias which is standard SQL.
Jacco Schalkwijk
SQL Server MVP
"Venkat" <venkat_kp@.yahoo.com> wrote in message
news:1092226310.253625@.sj-nntpcache-5...
> In a Union query, I have initialized one new column for sorting purpose.
> For
> ex :
> select ORD = 1 , t1.a, t1.b from table1 as t1
> Union
> select ORD = 0, t2.a, t2.b from table2 as t2
> order by ORD.
> ORD column is not present in thw table. It is used here for putting all
> the
> rows of the second query before the rows of first query. The query is
> getting executed in Windows which uses SQL 2000 server. But in
> Linux(RedHat)
> when we are using PostgresSQL database, the same query gives error -
> "Unable
> to parse 'ORD' ".
> Is this thing not supported in PostgresSQL? If this is not supported then
> what is the method of acheiving the same result?
>
> Thanks,
> Venkat
>
>
|||"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid > wrote
in message news:#YjU$q7fEHA.2848@.TK2MSFTNGP10.phx.gbl...
> Try:
> select 1 AS ORD , t1.a, t1.b from table1 as t1
> Union
> select 0, t2.a, t2.b from table2 as t2
> order by ORD
> column_alias = expression is T-SQL specific and not ANSI-SQL standard, as
> opposed to expression AS column_alias which is standard SQL.
>
Thanks Jacco it worked for me.
regards,
Venkat

PostgreSQL

In a Union query, I have initialized one new column for sorting purpose. For
ex :
select ORD = 1 , t1.a, t1.b from table1 as t1
Union
select ORD = 0, t2.a, t2.b from table2 as t2
order by ORD.
ORD column is not present in thw table. It is used here for putting all the
rows of the second query before the rows of first query. The query is
getting executed in Windows which uses SQL 2000 server. But in Linux(RedHat)
when we are using PostgresSQL database, the same query gives error - "Unable
to parse 'ORD' ".
Is this thing not supported in PostgresSQL? If this is not supported then
what is the method of acheiving the same result?
Thanks,
VenkatTry:
select 1 AS ORD , t1.a, t1.b from table1 as t1
Union
select 0, t2.a, t2.b from table2 as t2
order by ORD
column_alias = expression is T-SQL specific and not ANSI-SQL standard, as
opposed to expression AS column_alias which is standard SQL.
Jacco Schalkwijk
SQL Server MVP
"Venkat" <venkat_kp@.yahoo.com> wrote in message
news:1092226310.253625@.sj-nntpcache-5...
> In a Union query, I have initialized one new column for sorting purpose.
> For
> ex :
> select ORD = 1 , t1.a, t1.b from table1 as t1
> Union
> select ORD = 0, t2.a, t2.b from table2 as t2
> order by ORD.
> ORD column is not present in thw table. It is used here for putting all
> the
> rows of the second query before the rows of first query. The query is
> getting executed in Windows which uses SQL 2000 server. But in
> Linux(RedHat)
> when we are using PostgresSQL database, the same query gives error -
> "Unable
> to parse 'ORD' ".
> Is this thing not supported in PostgresSQL? If this is not supported then
> what is the method of acheiving the same result?
>
> Thanks,
> Venkat
>
>|||"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid> wrote
in message news:#YjU$q7fEHA.2848@.TK2MSFTNGP10.phx.gbl...
> Try:
> select 1 AS ORD , t1.a, t1.b from table1 as t1
> Union
> select 0, t2.a, t2.b from table2 as t2
> order by ORD
> column_alias = expression is T-SQL specific and not ANSI-SQL standard, as
> opposed to expression AS column_alias which is standard SQL.
>
Thanks Jacco it worked for me.
regards,
Venkat

Postal code search and LIKE statement

I'm trying to create a form that allows someone to find anaddress from a postal code search in the database. My query works exactly as I’dlike in query builder using the following select:

Select [FIELD_LIST] from addresses WHETE POSTCODE LIKE ‘%’+@.POSTCODE+’%’

I then pass the user entered post code to the select statementwhich is executed. However, I’m getting some odd behaviour. Assuming there isan address in the database with the post code “LS11 0ES”...

If I search for LS11, nothing is returned;

If I search for LS11%, nothing is returned;

If I search for %LS11%, nothing is returned;

If I search for %LS11%%, the data is returned.

If I search for %LS%1%%, the data is returned (as is LS210ES etc. etc.).

However, I want the user to be able to enter shorter searchstrings and it to pull all the data back out, so they can enter a substringsuch as LS and it will pull out all the data without the users needing to enterthe full pattern of % symbols.

If I go into query builder (in visual web developer) andenter just “LS” it works as I’d want, but not when pulled from a web page.

Any ideas?

Thanks

Look for other causes. I'm guessing somewhere in your code, you are removing the last % in postcode either before assigning it to the parameter, or modifying it during the selecting event.|||Thanks.At the moment I have a details view bound to an objectsource which in turn is bound to the above query and the parameter taken in from the text box’s .text property which the user types in.How can I trace where its failing?Thanks|||

A) response.write all your variables

or

B) Use a debugger like visual studio

sql

Tuesday, March 20, 2012

Post Deployment - Running Pkg With Set Values !!VOID

I was wondering if someone could provide me with a bit info on how to Set Values in the dtexecui?

I have a package that does a T-SQL SELECT query with a @.var, and that @.var needs to be set at runtime, in the Agent Job....

Thanks in advance.

Nevermind, having a slllllowwww moment this morning, need more coffee:)
In case others would like to know:

\Package.Variables[User::VAR_NAME].Properties[Value] : VAR_VALUE|||I've submitted a DCR to get a GUI interface as part of dtexecui that allows you to generate these property pathswithout typing them in yourself. It shouldn't be hard seeing as the same thing already exists in SSIS Designer.
In the meantime, the way to generate these property paths is using the XML configuration file wizard within SSIS Designer.

I'm sure you know this Jason :)

-Jamie|||Is it Feedback. Got a reference I can vote on? Drives me round the twist too.|||

DarrenSQLIS wrote:

Is it Feedback. Got a reference I can vote on? Drives me round the twist too.

Nah. I did it thru betaplace!

Post Deployment - Running Pkg With Set Values

I was wondering if someone could provide me with a bit info on how to Set Values in the dtexecui?

I have a package that does a T-SQL SELECT query with a @.var, and that @.var needs to be set at runtime, in the Agent Job....

Thanks in advance.

Nevermind, having a slllllowwww moment this morning, need more coffee:)
In case others would like to know:

\Package.Variables[User::VAR_NAME].Properties[Value] : VAR_VALUE|||I've submitted a DCR to get a GUI interface as part of dtexecui that allows you to generate these property pathswithout typing them in yourself. It shouldn't be hard seeing as the same thing already exists in SSIS Designer.
In the meantime, the way to generate these property paths is using the XML configuration file wizard within SSIS Designer.

I'm sure you know this Jason :)

-Jamie|||Is it Feedback. Got a reference I can vote on? Drives me round the twist too.|||

DarrenSQLIS wrote:

Is it Feedback. Got a reference I can vote on? Drives me round the twist too.

Nah. I did it thru betaplace!