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

Wednesday, March 28, 2012

new to mssql problem with query conversion

im managing most queries without any problems (im converting from access to mssql) but this one is causing me grief - how do i put this into mssql ?

SELECT dbo_Personal.ID, dbo_Personal.Surname1, dbo_Lead.SourceOfLead, dbo_Lead.DateOfLead, dbo_Mortgage.MortgageAppSubmitted, dbo_Mortgage.MortgageOfferedAccepted, dbo_Mortgage.MortgageDrawndown, dbo_Mortgage.MortgageApplicationClosed,
[dbo_Mortgage.MortgageCommissionAnticipated]+[dbo_Life.LifeCommissionAnticipated]+[dbo_BuildingsAndContents.BandCCommissionAnticipated]+[dbo_OtherBusiness.OtherBusinessCommissionAnticipated] AS Expr1,

[dbo_commissions.MortgageCommissionReceived]+[dbo_commissions.LifeCommissionReceived]+[dbo_commissions.BandCCommissionReceived]+[dbo_commissions.OtherBusinessCommissionReceived] AS Expr2

, IIf([Expr1]<1000,[Expr1]*0.3,IIf([Expr1]<2000,[Expr1]*0.4,[Expr1]*0.5)) AS Expr3
, IIf([Expr2]<1000,[Expr2]*0.3,IIf([Expr2]<2000,[Expr1]*0.4,[Expr2]*0.5)) AS Expr4

FROM (((((dbo_Personal INNER JOIN dbo_Lead ON dbo_Personal.ID = dbo_Lead.ID) LEFT JOIN dbo_Mortgage ON dbo_Personal.ID = dbo_Mortgage.ID) LEFT JOIN dbo_OtherBusiness ON dbo_Personal.ID = dbo_OtherBusiness.ID) LEFT JOIN dbo_BuildingsAndContents ON dbo_Personal.ID = dbo_BuildingsAndContents.ID) LEFT JOIN dbo_Commissions ON dbo_Personal.ID = dbo_Commissions.ID) LEFT JOIN dbo_Life ON dbo_Personal.ID = dbo_Life.ID
WHERE (((dbo_Lead.SourceOfLead) Like "Solutions*"));

Instead of this,
IIf([Expr1]<1000,[Expr1]*0.3,IIf([Expr1]<2000,[Expr1]*0.4,[Expr1]*0.5)) AS Expr3

Use


case
when [Expr1]<1000 then [Expr1]*0.3
when [Expr1]<2000 then [Expr1]*0.4
else [Expr1]*0.5
end as Expr3

similar syntax for Expr4 too.

Hope that helps.|||ill give it a go thanks!|||ive got as far as this :-

CREATE PROCEDURE solnorth AS

SELECT dbo.Personal.ID, dbo.Personal.Surname1, dbo.Lead.SourceOfLead, dbo.Lead.DateOfLead, dbo.Mortgage.MortgageAppSubmitted, dbo.Mortgage.MortgageOfferedAccepted, dbo.Mortgage.MortgageDrawndown, dbo.Mortgage.MortgageApplicationClosed,
dbo.Mortgage.MortgageCommissionAnticipated+dbo.Life.LifeCommissionAnticipated+dbo.BuildingsAndContents.BandCCommissionAnticipated+dbo.OtherBusiness.OtherBusinessCommissionAnticipated AS Expr1,
dbo.commissions.MortgageCommissionReceived+dbo.commissions.LifeCommissionReceived+dbo.commissions.BandCCommissionReceived+dbo.commissions.OtherBusinessCommissionReceived AS Expr2,

case
when Expr1<1000 then Expr1*0.3
when Expr1<2000 then Expr1*0.4
else Expr1*0.5
end as Expr3,

case
when Expr2<1000 then Expr2*0.3
when Expr2<2000 then Expr2*0.4
else Expr2*0.5
end as Expr4

FROM (((((dbo.Personal LEFT JOIN dbo.Lead ON dbo.Personal.ID = dbo.Lead.ID) LEFT JOIN dbo.Mortgage ON dbo.Personal.ID = dbo.Mortgage.ID) LEFT JOIN dbo.OtherBusiness ON dbo.Personal.ID = dbo.OtherBusiness.ID)
LEFT JOIN dbo.BuildingsAndContents ON dbo.Personal.ID = dbo.BuildingsAndContents.ID) LEFT JOIN dbo.Commissions ON dbo.Personal.ID = dbo.Commissions.ID)
LEFT JOIN dbo.Life ON dbo.Personal.ID = dbo.Life.ID
WHERE (((dbo.Lead.SourceOfLead) Like 'Solutions*'));
GO

but im getting invalid column name for Expr1 and Expr2 ?
where am i going wrong ?

thanks

mark|||You cannot use Expr1 and Expr2 (an alias) in the case. Just replace Expr1 and Expr2 with the same expressions you use to create Expr1 and Expr2.|||You will have to replace
Expr1
with
dbo.Mortgage.MortgageCommissionAnticipated+dbo.Life.LifeCommissionAnticipated+dbo.BuildingsAndContents.BandCCommissionAnticipated+dbo.OtherBusiness.OtherBusinessCommissionAnticipated

and

Expr2
dbo.commissions.MortgageCommissionReceived+dbo.commissions.LifeCommissionReceived+dbo.commissions.BandCCommissionReceived+dbo.commissions.OtherBusinessCommissionReceived|||great thanks that worked - well theres no errors now - just some errors with the datagrid but i should be able to sort that - thanks for the help everyone!|||quick (question how do i create Expr1 ?
eg

[dbo_Mortgage.MortgageCommissionAnticipated]+[dbo_Life.LifeCommissionAnticipated]+[dbo_BuildingsAndContents.BandCCommissionAnticipated]+[dbo_OtherBusiness.OtherBusinessCommissionAnticipated] AS Expr1

id still like to show expr1 on a datagrid if i could (eg add the columns together)

thanks

mark|||Umm, you create it by placing what you have in the select list. Am I missing something?|||heh my bad (im still learning!)
all works great now thanks for all the help

Wednesday, March 21, 2012

New SQL Query Issue...

Hello all,

Well I guess with any good application making comes SQL query problems as one trys to out-do what they did before. In any case, I am having an issue with sorting again, but this time its for a new field.

I have three tables, "Projects", "Users", and "Groups". Now the problem is this... a user can own a project, or a user could belong to a group, which owns the project. Whatever the case, a project will be owned by EITHER a group, or user, not both. So, when looping my Projects recordset, I have a second recordset that figures out if the GroupID is null in the Projects table, and if it is, it looks up the User's name in the Users table. If the GroupID isn't null, it grabs the Group name from the Group's table.

The problem is, I want to be able to do nice sorting with only one recordset? Is that possible?


ID Project Title Owner
1 Test Project XYZ Enterprises, Inc.
2 Another Project John Doe
3 Something Else Microsoft, Inc.

Thanks again!
TylerYou should be able to gather all of the data using one query and a CASE statement. I am assuming you have a UserID in your Projects table:


SELECT
Projects.ID,
Projects.Title,
CASE
WHEN Projects.GroupID IS NOT NULL THEN Groups.Name
ELSE
Users.Name
END AS Owner
FROM
Projects
LEFT OUTER JOIN
Groups ON Projects.GroupID = Groups.GroupID
LEFT OUTER JOIN
Users ON Projects.UserID = Users.UserID
ORDER BY
Projects.ID

Terri|||I will give that a shot! Thanks for introducing me to new SQL keywords. Do you have a site that I could resource to learn more about making complicated queries? It seems like I have to make them more and more.

Also, I have a third issue. In addition to the "Projects" and "Tasks" table that I'm sure your all too familiar with, I have a third level down called "Hours"... Now, this is the most complicated thing ever, so I doubt it is possible. But, I want to be able to pull out the earliest StartDate in the Hours table, and the last EndDate in the Hours table. And the hours table related to the "Tasks" table with the TaskID in both tables, and the "Tasks" relate to the "Projects" with the ProjectID in both tables. So, basically I want to look the Projects recordset, showing the Start and End dates for that project. Is that possible? It would have to be the highest and lowest dates. Not sure how to do all that action, especially while keeping the COALEASE thing, and this thing above!!

Thanks again,
B|||Ok, so my new SQL statement works, and looks like this so far:


SELECT Projects.*, Managers.LastName AS ManagersLastName, Managers.FirstName AS ManagersFirstName,
ROUND(COALESCE (SUM(Tasks.TaskStatusPercent) / COUNT(Tasks.TaskID), 0), 0) AS Status, CASE WHEN Projects.GroupID IS NOT NULL
THEN Groups.GroupName ELSE Users.FirstName END AS Client
FROM Projects INNER JOIN
Users Managers ON Projects.ManagerID = Managers.UserID LEFT OUTER JOIN
Users ON Projects.UserID = Users.UserID LEFT OUTER JOIN
Groups ON Projects.GroupID = Groups.GroupID LEFT OUTER JOIN
Tasks ON Tasks.ProjectID = Projects.ProjectID
GROUP BY Projects.ProjectID, Projects.UserID, Projects.GroupID, Projects.ManagerID, Projects.StatusID, Projects.StatusPercent, Projects.ProjectTitle,
Projects.ProjectURL, Projects.ProjectDescription, Projects.DateStarted, Projects.DateEnded, Managers.LastName, Managers.FirstName,
Users.FirstName, Groups.GroupName

The only problem is, instead of having just Users.FirstName, I would like the "Client" field to be "Users.LastName, Users.FirstName", with the comma and space in there. Can I do that in SQL too?

Also, if you could figure out the issue with the Hours.StartDate and Hours.EndDate, that would be the last thing I would need!

Thanks again,
B|||Ok, so I found out if you do...


RTrim(Users.LastName) + ', ' + LTrim(Users.FirstName) END AS Client

... it will work. Now, I just have to figure out that StartDate, EndDate issue.

- B|||Woo-hoo, you're almost there! I believe all you need for the StartDate and EndDate are a MIN and a MAX in your column list, and a LEFT OUTER JOIN to the Hours table.


SELECT Projects.*, Managers.LastName AS ManagersLastName, Managers.FirstName AS ManagersFirstName,
ROUND(COALESCE (SUM(Tasks.TaskStatusPercent) / COUNT(Tasks.TaskID), 0), 0) AS Status, CASE WHEN Projects.GroupID IS NOT NULL
THEN Groups.GroupName ELSE RTrim(Users.LastName) + ', ' + LTrim(Users.FirstName) END AS Client,
MIN(Hours.StartDate) AS StartDate,
MAX(Hours.EndDate) AS EndDate
FROM Projects INNER JOIN
Users Managers ON Projects.ManagerID = Managers.UserID LEFT OUTER JOIN
Users ON Projects.UserID = Users.UserID LEFT OUTER JOIN
Groups ON Projects.GroupID = Groups.GroupID LEFT OUTER JOIN
Tasks ON Tasks.ProjectID = Projects.ProjectID LEFT OUTER JOIN
Hours ON Tasks.TaskID = Hours.TaskID
GROUP BY Projects.ProjectID, Projects.UserID, Projects.GroupID, Projects.ManagerID, Projects.StatusID, Projects.StatusPercent, Projects.ProjectTitle,
Projects.ProjectURL, Projects.ProjectDescription, Projects.DateStarted, Projects.DateEnded, Managers.LastName, Managers.FirstName,
Users.FirstName, Groups.GroupName

Terri

New SQL 2005 - Where has Query Builder and Copy Objects gone?

Hello,

I am sure there will be a simple answer to this but it has got me stumped.

Having to move over to Vista with my new machine so I am having to switch to 2005 version for my development but still upload to a 2000 server.

I have had a look at 2005, like the new Management Studio, however I ahve a couple of problems which I can not find the answer to.

Firstly, the SQL Query Builder, where has it gone? I often have to import/export data from Excel files and used to use the SQL query builder to create my queries. If I want to copy all columns it is fine but if I want to import select columns I find it easier to view a list and then just add the ones I want.

Am I missing something here?

Secondly, copying stored procedures, before when running DTS ther were three options, Copy Tables/Views, Data Using Query and Copy Objects.

I used the copy opbjects a lot as it was a very quick way of transfering a group of tables and stored prcoedures that I had created. This appears to have now been replaced with Copy Database, which copies everthing, can can not be used to copy from SQL2005 to SQL2000.

If I want to copy multiple stored procedures from SQL2005 to SQL2000 how is it done now? I have tried finding out but have not been sucessful.

Any help would be greatly appreciated,

Regards,

Lee

Lee Redhead wrote:

Hello,

Firstly, the SQL Query Builder, where has it gone? I often have to import/export data from Excel files and used to use the SQL query builder to create my queries. If I want to copy all columns it is fine but if I want to import select columns I find it easier to view a list and then just add the ones I want.

When run Management Studio open Database Engine Server , you'll have all the server objects in an explorer view, then "New query..." to open a window to write a select.

Lee Redhead wrote:

If I want to copy multiple stored procedures from SQL2005 to SQL2000 how is it done now? I have tried finding out but have not been sucessful.

Try database right click->Tasks->Generate Scripts

Or
create a new Integration Services project with SQL Server Business Intelligence Studio and use "Transfer SQL Server Objects Task" ; with it you can transfer all objects needed.|||

The Query Builder is a bit 'hidden'.

In Object Explorer, right-click on the primary table, select [Open Table].

In the output pane, if the table is large, in order to 'stop' the data from displaying, click on the RED 'stop' button at the bottom of the pane. (Yes, we know that this is a pain, and it will most likely be fixed -but that is the way it is for now.)

At the upper left of the toolbar, click on the [SQL], [Criteria],[Diagram] panes as needed.

Again, in the Object Explorer, right-click on the database name, and select [Tasks]. There you can select either -Export Data to move tables and data to another server, or [Generate Scripts] to script out Stored Procedures and Functions, then EXECUTE that script in the other server/database. Explore and follow the Wizard prompts.

Monday, March 19, 2012

New Query with Current Connection

I am missing "New Query with Current Connection" button in SQL Editor in the
SQL management studio. Few posts on the Web refer explain that this button
should allow me to open a new query in the existing window.
I've checked "Add Remove Buttons" for SQL editor but I still cannot find it.
Any ideas?
Bojan Kuhar wrote:
> I am missing "New Query with Current Connection" button in SQL Editor in the
> SQL management studio. Few posts on the Web refer explain that this button
> should allow me to open a new query in the existing window.
> I've checked "Add Remove Buttons" for SQL editor but I still cannot find it.
> Any ideas?
I'm not sure it can be done, but "Ctrl-N" will give you the "New query
with current connection".
Regards
Steen
|||Fantastic.
Now, one more. Is there something like "Open Query with Current Connection".
I'd like to avoid the Connect to Database Enginde dialog.
More-less like Qury Analyzer that allows you to open a new query in the
existing window.
Regards
Bojan
"Steen Persson (DK)" wrote:

> Bojan Kuhar wrote:
> I'm not sure it can be done, but "Ctrl-N" will give you the "New query
> with current connection".
> Regards
> Steen
>
|||My fault. The button was called New Query with Current Connection in one of
the betas. The released version renamed it to New Query. I changed most of
the references, but I missed a few. I think I got rid of them all by web
release 1 of Books Online though.
The New Query button will use the connection that has focus when you click
it. Either Object Explorer, or an existing Query Editor window. If your
current context has no connection, then it will pop up the Connect to Server
dialog box.
Rick Byham
MCDBA, MCSE, MCSA
Documentation Manager,
Microsoft, SQL Server Books Online
This posting is provided "as is" with
no warranties, and confers no rights.
"Bojan Kuhar" <BojanKuhar@.discussions.microsoft.com> wrote in message
news:01230DB5-BEE5-4452-B89E-470DCCE488F2@.microsoft.com...
>I am missing "New Query with Current Connection" button in SQL Editor in
>the
> SQL management studio. Few posts on the Web refer explain that this
> button
> should allow me to open a new query in the existing window.
> I've checked "Add Remove Buttons" for SQL editor but I still cannot find
> it.
> Any ideas?
|||Bojan Kuhar (BojanKuhar@.discussions.microsoft.com) writes:
> Fantastic.
> Now, one more. Is there something like "Open Query with Current
> Connection". I'd like to avoid the Connect to Database Enginde dialog.
> More-less like Qury Analyzer that allows you to open a new query in the
> existing window.
What Steen said, CTRL-N is the key. Provided one thing: under Tools->
Options->Keyboard select SQL 2000 instead of Standard.
The New Query button also works if you are in a query window or Object
Explorer, and so does File->New->Query with Current Connection.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

New Query Connect Dialog

The "Microsoft SQL Server Management Studio" opens a connection dialog with every new query. Please tell me there is setting to prevent this.

Thanx,

Greg

The popup of the dialog is related to your current context. If you have object explorer open and a server connected then the query will be connected to that server. If you have an existing query window connected it should connect to that server.

If working disconnected then you will get the pop up.

|||

Thank you for your reply, your answer does work nicely. It would seem this would be something you could define a default for. It is taking a little time to get used to the SQL 2005 tools after using SQL 2000 for so long.

If you are a member of Experts-Exchange.com please visit this link and post your answer to receive the allotted points. If after 48 hours or so I have not seen your post there I will post your answer for the benefit of others and close the question in that forum.

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21773295.html

Regards,

Greg

|||Done, haven't answered a question on EE for a while|||

EE Saves my bacon from time to time:)

Your blog site is very kewl, you must be a busy person.

Sent an email to your yahoo address Returned Undeliverable.

|||I think this is semi-related to your thread, so I hope it's cool to post this here. If you have a query window in SSMS, and drag and drop one or more script files on it, it prompts you for the database to connect to, for each file you dropped. In 2000 query analyzer, it will automatically connect each file to the same server and database. Do you know of a way to accomplish this in 2005?|||I don't believe so.

New Query Connect Dialog

The "Microsoft SQL Server Management Studio" opens a connection dialog with every new query. Please tell me there is setting to prevent this.

Thanx,

Greg

The popup of the dialog is related to your current context. If you have object explorer open and a server connected then the query will be connected to that server. If you have an existing query window connected it should connect to that server.

If working disconnected then you will get the pop up.

|||

Thank you for your reply, your answer does work nicely. It would seem this would be something you could define a default for. It is taking a little time to get used to the SQL 2005 tools after using SQL 2000 for so long.

If you are a member of Experts-Exchange.com please visit this link and post your answer to receive the allotted points. If after 48 hours or so I have not seen your post there I will post your answer for the benefit of others and close the question in that forum.

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21773295.html

Regards,

Greg

|||Done, haven't answered a question on EE for a while|||

EE Saves my bacon from time to time:)

Your blog site is very kewl, you must be a busy person.

Sent an email to your yahoo address Returned Undeliverable.

|||I think this is semi-related to your thread, so I hope it's cool to post this here. If you have a query window in SSMS, and drag and drop one or more script files on it, it prompts you for the database to connect to, for each file you dropped. In 2000 query analyzer, it will automatically connect each file to the same server and database. Do you know of a way to accomplish this in 2005?|||I don't believe so.

Monday, March 12, 2012

New query based on SOME fields returned in another query

I need to have two queries, the second query uses some information from the first query but the second query's results is not a subset of the first query.

this is my first query

select name, date, company, valid
from tbl1, tbl2, tbl3
where valid=0

Then I need to take the person's name to do another query, but I don't need to look in the first set of results to get what I need. It is a brand new query just with the name as the parameter.

select name
from tbl4
where name = @.name

Right now I have the second query getting it's parameter from the first query in RS. The two queries are two different data sets BUT my second query is limited by the results from the first query. How do I get the second query to be it's own query not based on the first but get a parameter from the first query.

I know this is all very confusing. I apologize. I am trying to explain fully.

Please help, I am new to RS and quite new in SQL

Well, I think I solved my problem. I needed to create a sub report.

New project - thinking of using Visual studio 2005

Hi
I've been developing sql server stored procedures for what seems forever,
right now I just use query analyzer.
I have a new project, and just for a chuckle I'm thinking of using Visual
Studio 2005 for my IDE instead of query analyzer, I'm still pretty much just
going to be creating sql server stored procedures (SQL 2K).
Does anybody have any hints, gotcha's or guidance on whether this is a good
idea, and if so any tips?
I've played around, and one thing I can't find, can I run a SQL and have a
nice output to grid option, like with query analyzer?
The main reason I want to use this, is for being able to put all my sql in a
project, and the integration with sourcesafe.
Thanks in advanceInstead of the 'full' Visual Studio, use the Sql Server Management Studio.
When you disable the 'Summary' tab at startup, it behaves more or less the
same way as ye olde QA, but a little better :)
Yes, you can still have grids and text output :)
Peter
"..." <...@.nowhere.com> wrote in message
news:ehgLtTgQGHA.4896@.TK2MSFTNGP10.phx.gbl...
> Hi
> I've been developing sql server stored procedures for what seems forever,
> right now I just use query analyzer.
> I have a new project, and just for a chuckle I'm thinking of using Visual
> Studio 2005 for my IDE instead of query analyzer, I'm still pretty much
> just going to be creating sql server stored procedures (SQL 2K).
> Does anybody have any hints, gotcha's or guidance on whether this is a
> good idea, and if so any tips?
> I've played around, and one thing I can't find, can I run a SQL and have a
> nice output to grid option, like with query analyzer?
> The main reason I want to use this, is for being able to put all my sql in
> a project, and the integration with sourcesafe.
> Thanks in advance
>|||There is more flexibility within visual studio itself wrt managing your
project (you can add more folders for managing DDL/DML scripts and such).
Typically I manage my project and the source control integration from within
VS and jump back and forth to management studio depending on the specific
task at hand (say building up and testing a specific set of queries within a
larger procedure). SQL management studio allows for source control
integration and projects but is slightly different. The overall impression
I
have gotten from the two is that the SQL management studio projects are
geared more toward DBA work where as VS is more for the DB developer.
HTH
--Tony
"Rogas69" wrote:

> Instead of the 'full' Visual Studio, use the Sql Server Management Studio.
> When you disable the 'Summary' tab at startup, it behaves more or less the
> same way as ye olde QA, but a little better :)
> Yes, you can still have grids and text output :)
> Peter
> "..." <...@.nowhere.com> wrote in message
> news:ehgLtTgQGHA.4896@.TK2MSFTNGP10.phx.gbl...
>
>

new patch for 32 bit and 64 bit SQL server

How do you determine if a server is running the 32 bit or
64 bit version. I have seen the query to determine service
pack level but I don't see an indicator to determine if a
server is 32 bit or 64 bit. There is a different patch
dependant on this value.>--Original Message--
>Pat,
>Is it the season? lol :-)
>I just answered a similar query a while back...
>--
>Dinesh.
>SQL Server FAQ at
>http://www.tkdinesh.com
>"Pat" <mcilweep@.ctbsonline.com> wrote in message
>news:01ee01c352d6$7d4ca5c0$a301280a@.phx.gbl...
>> How do you determine if a server is running the 32 bit
or
>> 64 bit version. I have seen the query to determine
service
>> pack level but I don't see an indicator to determine if
a
>> server is 32 bit or 64 bit. There is a different patch
>> dependant on this value.
>
>.
>I appreciate your reply but still don't see the info I
need. I found the info about determining the SP of SQL on
your website FAQ's, but I don't see anything that
indicates that it is a 32 bit or 64 bit system. What am I
missing?|||Pat,
May be I got carried away because I answered couple of similar posts
one-by-one in .server :-) The latest one was by Aparna Rege.Anyways, this is
the info:
You should be able to tell by running a SELECT @.@.version against your SQL
Server.
On 32-bit, you will see the following:
Microsoft SQL Server 2000 - 8.00.818 (Intel X86)
May 31 2003 16:08:15
Copyright (c) 1988-2003 Microsoft Corporation
Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 1)
On 64-bit, the architecture should be Intel IA-64
Microsoft SQL Server 2000 - 8.00.818 (Intel IA-64)
Thanks to : Arvind Krishnan
--
Dinesh.
SQL Server FAQ at
http://www.tkdinesh.com
"Pat" <mcilweep@.ctbsonline.com> wrote in message
news:03cb01c352e6$10f67990$a101280a@.phx.gbl...
> >--Original Message--
> >Pat,
> >
> >Is it the season? lol :-)
> >
> >I just answered a similar query a while back...
> >
> >--
> >Dinesh.
> >SQL Server FAQ at
> >http://www.tkdinesh.com
> >
> >"Pat" <mcilweep@.ctbsonline.com> wrote in message
> >news:01ee01c352d6$7d4ca5c0$a301280a@.phx.gbl...
> >> How do you determine if a server is running the 32 bit
> or
> >> 64 bit version. I have seen the query to determine
> service
> >> pack level but I don't see an indicator to determine if
> a
> >> server is 32 bit or 64 bit. There is a different patch
> >> dependant on this value.
> >
> >
> >.
> >I appreciate your reply but still don't see the info I
> need. I found the info about determining the SP of SQL on
> your website FAQ's, but I don't see anything that
> indicates that it is a 32 bit or 64 bit system. What am I
> missing?

New partition (SSAS 2005) - named query not in "available tables"

I'm trying to create another partition in an existing measure group.
I start the new partition wizard and choose my data source view in the
"Look in", and then click "Find tables".
My named query does not appear in the list. What could be wrong?

I have saved and refreshed the data source view. The named query has the
same column names and data types that the original table has (that the
measure group is using now).

I also tried to make the named query a ordinary view in the source
database but that does not appear in the list either (I choose the
data source then instead of the data source view).

Hi,

I wouldn't suspect that you'd see a named query in the list of tables when you go to create a new partition. I'm not sure if that list of tables shows views from the underlying DB, either..But, you can make your partition query binding, instead of table binding, and you can make the partition source the same query you're using in your named query, or a subset of it with a where clause, or whatever you choose.

HTH,
C
|||

I don't understand...

In the "New partition wizard" there isn't a whole lot to choose from... Pick either a data source or a data source view, and then pick the "table" - which I suppose could be a view, table, named query... When I choose data source view I can only see the table that is already being used as a fact table in the measure group (and this is actually a named query and not a table). When I choose the data soure, eg. the underlying database, I get nothing.

|||

I found the answer myself.

I thought I had the same datatypes because I created my new named query from the original table that the first named query (the one being used in the measure group already) was based on. But it turned out that the first named query did not get the same datatypes i all columns as the underlying table.

In BOL it states that the tables (or named queries) used in the same measure group must be sufficiently similar - I would call it exactly the same :-)

Friday, March 9, 2012

New Log or append?

I'm backing into the DBA position from being an app / web designer. Using SQL 2005 and hating wizards, I'm trying to code a BACKUP LOG query to run every 15 minutes and create a separate date-time-stamped TRN. I have several questions starting with - should I be doing this? Is it better to create separate LOGs or append to one? Isn't it true that the only time I'll need the LOGs is when I have a crash and that then I'll need to RESTORE in date-time-order?

I'm doing a FULL backup every 6 hours and intend on automating deleting the TRNs on a successful completion of the BAK.

The following code runs fine and creates a date-time-stamped TRN but doesn't do what I want, as it overwrites the first TRN, so how do I create, say, four JOBSTEPs and get a JOBSTEP @.COMMAND to run the SETs?

USE msdb

IFEXISTS(SELECTNameFROM sysjobs WHEREName='jobDBBackupWedb_1Log')

EXECsp_delete_job @.job_name ='jobDBBackupWedb_1Log'

GO

DECLARE @.now char(14)-- current date in the form of yyyymmddhhmmss

DECLARE @.dbName sysname-- database name to include date-time-stamp

DECLARE @.Cmd nvarchar(260)-- variable used to build JobStep command

SET @.now =REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(50),GETDATE(), 120),'-',''),' ',''),':','')

SET @.dbName ='Wedb_1_'+ @.now +'_.trn'

SET @.Cmd ='BACKUP LOG Wedb_1 TO DISK = '+CHAR(39)+'\\SERVER1\D$\SQLDatabases\Backup\WebEoc\'+ @.dbName +CHAR(39)

EXECsp_add_job

@.Job_Name ='jobDBBackupWedb_1Log',

@.Description ='Run Wedb_1 DB Backup Transaction Log Every 15 Minutes',

@.Category_Name ='Database Maintenance'

EXECsp_add_jobstep

@.Job_Name ='jobDBBackupWedb_1Log',

@.step_name ='DB_Backup_Log',

@.step_id = 1,

@.Database_Name ='Master',

@.subsystem ='TSQL',

@.command = @.Cmd

/*

Start time for the Transaction Log backups is seven and one half minutes after midnight

to not conflict with the Full database backups run every 6 hours starting at midnight.

The Transaction Log backups are to be run every fifteen minutes.

*/

EXECsp_Add_JobSchedule

@.Job_Name ='jobDBBackupWedb_1Log',

@.Name ='schedDB_BackupLog',

@.Freq_Type = 4,-- Daily

@.Freq_Interval = 1,-- Each day

@.Freq_Subday_Type = 0x4,-- Interval in minutes

@.Freq_Subday_Interval = 15,-- Every fifteen minutes

@.Active_Start_Time = 000730 -- Start Time is seven and one half minutes after midnight

--@.Active_End_Time = 235959 -- End Time is next midnight

EXECsp_Add_JobServer

@.Job_Name ='jobDBBackupWedb_1Log'

GO

My opinion, I'd create DB Maint Plans to do database and trans log backups, and not write my own, unless there is some real good reason to. Main plans aren't perfect, but still better then trying to reproduce the same end result. One of the points of backing up the trans log is for disaster recovery. You could backup your trans log every 30 minutes, then copy/move those backups off to a different server/tape, etc... So, I wouldn't stack them on one file, becuase you need to move them continually to a different box, or yea, backup cross server, ehhhh.... I prefer local backups and then copying them somewhere else, like with RoboCopy. The times you nee the trans logs are for recovery, or to do log shipping to a different server (similar concept to database mirroring, but still more in use then mirroring is I think). If doing a full backup every 6 hours, same thing, back it up and move it off somewhere safe, not leave that on teh same drives as your database files! I'd still leave the old TRN files on teh other location, there COULD be a case where you need to go back 9 hours, then you'd have the old trans and backups to get there, I wouldnt delete the TRN files every 6 hours as you said.

Bruce

|||

Thanks ... but ...

I'm backing up to a separate / different network drive on a different / separate server that my network admin has established as a SANs - Storage Area Network - drive. That SANs drive is data specific and is being snap-shot-backed-up every 5 minutes. Then it goes to tape every night at 11:00 PM.

The nature of the DB is that the WebEoc is for running the SEOC - State Emergency Operations Center - during hurricanes, earthquakes, etc.

|||

I think that the problem with your original script is that you are creating the backup command when you create the job rather than dynamically creating and executing the command when executing the job.

Try the re-worked version below.

Chris

Code Snippet

USE msdb

IF EXISTS ( SELECT Name
FROM sysjobs
WHERE Name = 'jobDBBackupWedb_1Log' )
EXEC sp_delete_job @.job_name = 'jobDBBackupWedb_1Log'

GO

DECLARE @.cmd NVARCHAR(3201)

SET @.cmd = N'DECLARE @.sql VARCHAR(8000)

DECLARE @.now char(14)
-- current date in the form of yyyymmddhhmmss

DECLARE @.dbName sysname
-- database name to include date-time-stamp

SET @.now = REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(50), GETDATE(), 120), ''-'',
''''), '' '', ''''), '':'', '''')

SET @.dbName = ''Wedb_1_'' + @.now + ''_.trn''

SET @.sql = ''BACKUP LOG Wedb_1 TO DISK = '' + CHAR(39)
+ ''\\SERVER1\D$\SQLDatabases\Backup\WebEoc\'' + @.dbName + CHAR(39)

EXEC(@.sql)'
EXEC sp_add_job @.Job_Name = 'jobDBBackupWedb_1Log',
@.Description = 'Run Wedb_1 DB Backup Transaction Log Every 15 Minutes',
@.Category_Name = 'Database Maintenance'

EXEC sp_add_jobstep @.Job_Name = 'jobDBBackupWedb_1Log',
@.step_name = 'DB_Backup_Log', @.step_id = 1, @.Database_Name = 'Master',
@.subsystem = 'TSQL', @.command = @.Cmd

/*

Start time for the Transaction Log backups is seven and one half minutes after midnight

to not conflict with the Full database backups run every 6 hours starting at midnight.

The Transaction Log backups are to be run every fifteen minutes.

*/

EXEC sp_Add_JobSchedule @.Job_Name = 'jobDBBackupWedb_1Log',
@.Name = 'schedDB_BackupLog', @.Freq_Type = 4, -- Daily
@.Freq_Interval = 1, -- Each day
@.Freq_Subday_Type = 0x4, -- Interval in minutes
@.Freq_Subday_Interval = 15, -- Every fifteen minutes
@.Active_Start_Time = 000730
-- Start Time is seven and one half minutes after midnight

--@.Active_End_Time = 235959 -- End Time is next midnight

EXEC sp_Add_JobServer @.Job_Name = 'jobDBBackupWedb_1Log'

GO

|||

Chris,

Thank you very kindly! I take your comment about run-time, create/execute; that was spot on. Thanks for the code change - works like a charm ...

... but, I've looked and not found an answer, what is the "N" doing for me in SET @.cmd = N'DECLARE ... ?

Is it declaring that what follows is all an NVARCHAR string?

New line in query

Hi,
I was wondering if there is any way I can place a new line inside a query...
e.g.
select field1 + 'NEWLINE' + field2 from tablename
I want to place a new line between field1 and field2
Thanks in advanceselect field1 + char(13) + field2 from tablename|||Thanks for reply blindman

I have tried this already but it doesn't work :(
any other suggestions would be appreciated.

Thanks|||It works in query analyzer. What are you looking at the results in?

select 'a' + char(13) + 'b'|||I am using MSSQL 2005 Server Management Studio which replaces both the SQL Server 2000 Enterprise Manager and the Query Analyzer.

Thanks|||Odd. It still works for me.

select 'a' + char(13) + 'b'

--
a
b

(1 row(s) affected)|||Actually It works on the text mode result but not Grid mode ...still it doesn't save a and b on different lines in the database itself not sure why

Once I fetch the record it displays it on a single line...

On my other fields where I am saving text from my .net application textboxes it creates double boxes for a newline... I was wondering how would I create thoes double boxes :)|||Ahh, the grid is saving the carriage return, but it displays as a blank in grid mode.

-- Using Northwind

DECLARE @.MyText CHAR(26)
DECLARE @.MyText2 CHAR(26)
SET @.MyText = 'a' + char(13) + 'b'
PRINT @.MyText

SELECT ShipCity
FROM Orders
WHERE OrderID = 10248

UPDATE Orders
SET ShipCity = @.MyText
WHERE OrderID = 10248

SET @.MyText2 = (SELECT ShipCity
FROM Orders
WHERE OrderID = 10248)

PRINT @.MyText2

SELECT ShipCity
FROM Orders
WHERE OrderID = 10248

UPDATE Orders
SET ShipCity = 'Reims'
WHERE OrderID = 10248

Run this in text mode and you will see that the newline is saved.
Run this in grid mode and you will see that the newline is converted.

I hope this helps,|||Still it will show a one straight line when fetching that data ....what I am thinking now is create a tiny function in .net and add new lines there it did worked for my other data before....

"thank you all for the help guys this forum ROCKS :)"|||Why are you concerned with how it looks in Management Studio? Neither Management Studio or Query Analyzer is meant to be used as a user interface or reporting tool.

Saturday, February 25, 2012

New entries in db query

Our SQL guy is off on vacation and I have been asked to write a query that
identifies new entries to our db. We have a table in a SQL 2000 db that has
a
list of computer IDs, comments and date of entry. There can, and usually is,
multiple entries of comments for each computer.
What I have been asked to extract is if a new Computer ID is entered.
Ex. My machine name is: workstation123. It already has several entries in
the database which are already handled. If workstation124 gets an entry and
it is not already in the db - I need to be able to query that.
The table is simple and has only 4 columns:
CompName - nvarchar
TheDate - datetime
TheComment - ntext
TheID - int {Identity}
Any idea how I can extract all of the ComputerIDs {CompName} on the most
recent date {TheDate} that do not appear anywhere else in the table {all
other dates}?
ThanksHello
You can use something like this:
DECLARE @.LastDate datetime
SELECT @.LastDate=MAX(TheDate) FROM YourTable
SELECT DISTINCT CompName FROM YourTable
WHERE TheDate=@.LastDate AND CompName NOT IN (
SELECT CompName FROM YourTable WHERE TheDate<@.LastDate
)
I assume that you store only the date in TheDate (not the date and the
time). If you also store the time, the query needs to be modified to
handle it correctly.
Razvan|||Define "new". Does it mean that a new computer has one - and only one -
comment? Does it mean that only one comment occurred on a specific date?
Could you give us some sample data and pick the "new" computer?
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"vm" <vm@.discussions.microsoft.com> wrote in message
news:2521A071-50C1-49FE-B35C-46F5A3A0732B@.microsoft.com...
Our SQL guy is off on vacation and I have been asked to write a query that
identifies new entries to our db. We have a table in a SQL 2000 db that has
a
list of computer IDs, comments and date of entry. There can, and usually is,
multiple entries of comments for each computer.
What I have been asked to extract is if a new Computer ID is entered.
Ex. My machine name is: workstation123. It already has several entries in
the database which are already handled. If workstation124 gets an entry and
it is not already in the db - I need to be able to query that.
The table is simple and has only 4 columns:
CompName - nvarchar
TheDate - datetime
TheComment - ntext
TheID - int {Identity}
Any idea how I can extract all of the ComputerIDs {CompName} on the most
recent date {TheDate} that do not appear anywhere else in the table {all
other dates}?
Thanks|||> Any idea how I can extract all of the ComputerIDs {CompName} on the most
> recent date {TheDate} that do not appear anywhere else in the table {all
> other dates}?
declare @.target datetime
set @.target = '20060220'
select CompName, min(TheDate) as CreatedDate
from '
group by CompName
having min(TheDate) >= @.target
This assumes that there are no rows where TheDate is in the future (in the
example, great than Feb 21 2006 00:00:00.000). This also assumes that the
TheDate column is either datetime or smalldatetime.|||Thanks Raz, your code was spot on. I am not the SQL person, but I created a
test table with some sample data like the live data. I changed the data
around for different situations we would encounter... Your code did
everything they were looking for...
Thanks again and thanks to all who gave suggestions!
vm
"vm" wrote:

> Our SQL guy is off on vacation and I have been asked to write a query that
> identifies new entries to our db. We have a table in a SQL 2000 db that ha
s a
> list of computer IDs, comments and date of entry. There can, and usually i
s,
> multiple entries of comments for each computer.
>
> What I have been asked to extract is if a new Computer ID is entered.
>
> Ex. My machine name is: workstation123. It already has several entries in
> the database which are already handled. If workstation124 gets an entry an
d
> it is not already in the db - I need to be able to query that.
>
> The table is simple and has only 4 columns:
> CompName - nvarchar
> TheDate - datetime
> TheComment - ntext
> TheID - int {Identity}
>
> Any idea how I can extract all of the ComputerIDs {CompName} on the most
> recent date {TheDate} that do not appear anywhere else in the table {all
> other dates}?
> Thanks

Monday, February 20, 2012

New Database Name not visible while DSN setup

Hi,
I have taken backup(connectdb.bak) from production and
created the database in test server. For that I used
following procedure,
In sql query analyzer, I executed the following two steps
consecutively.
1. RESTORE FILELISTONLY FROM DISK = 'c:\Mssql7
\Backup\connectdb.bak'
2. RESTORE DATABASE newdb FROM DISK = 'c:\Mssql7
\Backup\connectdb.bak' WITH
MOVE 'connectdb_Data' TO 'c:\test\newdb.mdf',
MOVE 'connectdb_log' TO 'c:\test\newdb_log.ldf'
This created a new database "newdb" in test server from
the back up file connectdb.bak.
I want to connect to this database.
Now while going through SQL server DSN configuration,
there comes an option where we get the following option,
"change the default database to"
While clicking in this box,we get a drop down listing of
all the databases. Here I am stuck. Ideally the "newdb"
database which I have created from connectdb.bak file,
should be visible here so that I can choose to connect to
it. But it's not visible here and unless it's visible here
I can't do anything. The otherdatabases which were present
in the test server since beginning are visible accept this
one.
Please help. It's urgent
regards,
Vishal
Check to see if the login/user for whom you are setting up
the DSN is a user in the database that has been restored. It
sounds like they users could have been orphaned.
You can find a good article on orphaned users and
troubleshooting techniques, links to KB articles at:
http://vyaskn.tripod.com/troubleshoo...phan_users.htm
-Sue
On Thu, 1 Apr 2004 08:40:14 -0800, "vishal kumar"
<anonymous@.discussions.microsoft.com> wrote:

>Hi,
>I have taken backup(connectdb.bak) from production and
>created the database in test server. For that I used
>following procedure,
>In sql query analyzer, I executed the following two steps
>consecutively.
>1. RESTORE FILELISTONLY FROM DISK = 'c:\Mssql7
>\Backup\connectdb.bak'
>2. RESTORE DATABASE newdb FROM DISK = 'c:\Mssql7
>\Backup\connectdb.bak' WITH
>MOVE 'connectdb_Data' TO 'c:\test\newdb.mdf',
>MOVE 'connectdb_log' TO 'c:\test\newdb_log.ldf'
>This created a new database "newdb" in test server from
>the back up file connectdb.bak.
>I want to connect to this database.
>Now while going through SQL server DSN configuration,
>there comes an option where we get the following option,
>"change the default database to"
>While clicking in this box,we get a drop down listing of
>all the databases. Here I am stuck. Ideally the "newdb"
>database which I have created from connectdb.bak file,
>should be visible here so that I can choose to connect to
>it. But it's not visible here and unless it's visible here
>I can't do anything. The otherdatabases which were present
>in the test server since beginning are visible accept this
>one.
>Please help. It's urgent
>regards,
>Vishal
>
|||If this is a new serer then the user exists in the
database but not in master. You first need to create the
login. Then synch up the users with sp_change_users_login
>--Original Message--
>Check to see if the login/user for whom you are setting up
>the DSN is a user in the database that has been restored.
It
>sounds like they users could have been orphaned.
>You can find a good article on orphaned users and
>troubleshooting techniques, links to KB articles at:
>http://vyaskn.tripod.com/troubleshoo...phan_users.htm
>-Sue
>On Thu, 1 Apr 2004 08:40:14 -0800, "vishal kumar"
><anonymous@.discussions.microsoft.com> wrote:
steps
to
here
present
this
>.
>

New Database Name not visible while DSN setup

Hi,
I have taken backup(connectdb.bak) from production and
created the database in test server. For that I used
following procedure,
In sql query analyzer, I executed the following two steps
consecutively.
1. RESTORE FILELISTONLY FROM DISK = 'c:\Mssql7
\Backup\connectdb.bak'
2. RESTORE DATABASE newdb FROM DISK = 'c:\Mssql7
\Backup\connectdb.bak' WITH
MOVE 'connectdb_Data' TO 'c:\test\newdb.mdf',
MOVE 'connectdb_log' TO 'c:\test\newdb_log.ldf'
This created a new database "newdb" in test server from
the back up file connectdb.bak.
I want to connect to this database.
Now while going through SQL server DSN configuration,
there comes an option where we get the following option,
"change the default database to"
While clicking in this box,we get a drop down listing of
all the databases. Here I am stuck. Ideally the "newdb"
database which I have created from connectdb.bak file,
should be visible here so that I can choose to connect to
it. But it's not visible here and unless it's visible here
I can't do anything. The otherdatabases which were present
in the test server since beginning are visible accept this
one.
Please help. It's urgent
regards,
VishalCheck to see if the login/user for whom you are setting up
the DSN is a user in the database that has been restored. It
sounds like they users could have been orphaned.
You can find a good article on orphaned users and
troubleshooting techniques, links to KB articles at:
http://vyaskn.tripod.com/troublesho...rphan_users.htm
-Sue
On Thu, 1 Apr 2004 08:40:14 -0800, "vishal kumar"
<anonymous@.discussions.microsoft.com> wrote:

>Hi,
>I have taken backup(connectdb.bak) from production and
>created the database in test server. For that I used
>following procedure,
>In sql query analyzer, I executed the following two steps
>consecutively.
>1. RESTORE FILELISTONLY FROM DISK = 'c:\Mssql7
>\Backup\connectdb.bak'
>2. RESTORE DATABASE newdb FROM DISK = 'c:\Mssql7
>\Backup\connectdb.bak' WITH
>MOVE 'connectdb_Data' TO 'c:\test\newdb.mdf',
>MOVE 'connectdb_log' TO 'c:\test\newdb_log.ldf'
>This created a new database "newdb" in test server from
>the back up file connectdb.bak.
>I want to connect to this database.
>Now while going through SQL server DSN configuration,
>there comes an option where we get the following option,
>"change the default database to"
>While clicking in this box,we get a drop down listing of
>all the databases. Here I am stuck. Ideally the "newdb"
>database which I have created from connectdb.bak file,
>should be visible here so that I can choose to connect to
>it. But it's not visible here and unless it's visible here
>I can't do anything. The otherdatabases which were present
>in the test server since beginning are visible accept this
>one.
>Please help. It's urgent
>regards,
>Vishal
>|||If this is a new serer then the user exists in the
database but not in master. You first need to create the
login. Then synch up the users with sp_change_users_login
>--Original Message--
>Check to see if the login/user for whom you are setting up
>the DSN is a user in the database that has been restored.
It
>sounds like they users could have been orphaned.
>You can find a good article on orphaned users and
>troubleshooting techniques, links to KB articles at:
>http://vyaskn.tripod.com/troublesho...rphan_users.htm
>-Sue
>On Thu, 1 Apr 2004 08:40:14 -0800, "vishal kumar"
><anonymous@.discussions.microsoft.com> wrote:
>
steps
to
here
present
this
>.
>