Showing posts with label create. Show all posts
Showing posts with label create. Show all posts

Wednesday, March 28, 2012

Mapping Column Headers from Source to Rows in a spreadsheet

Hello,

I am trying to do the following:

I have been given an MS Access Database that has a table with columns

I have to create a spreadsheet that will have the data stored in the column header as a row (essentially we are creating a spreadsheet that records all of the different columns in all of the different tables in the MS Access DB).

Any suggestions?

Where the problem is?

Mapping a user to a login

I have created a login and a user on a database using SMO. I am having trouble mapping a database to the login. When you create a login from the GUI using SQL Server Management Studio Express CTP the user is automatically created and the database is mapped to this login from the user mapping tab. When I create the login programmatically this is not the case so I need to create the user. How do I map the database to the login. I set the default database property on the login but this does not do it.

Thanks,

Michael Gustafson

This is the pattern you would normally follow (this is for a windows user, but it works the same for a SQL user):

Login l = new Login(svr, @."REDMOND\mwories");
l.LoginType = LoginType.WindowsUser;
l.Create();

Database db = svr.Databases["AdventureWorks"];

User u = new User(db, @."REDMOND\mwories");
u.Login = l.Name;
u.UserType = UserType.SqlLogin;
u.Create();

This maps the database user to the login as you create the login.

|||

what property of the Login is used to show what databases are mapped?

also is there a way with SMO to check and see if a password is blank? Granted the odds of that are extemely slimmer with 2005.

|||

I found the DatabaseMapping() class and was able to find which logins were missing mappings. Im still at a lost at how to test for blank passwords using SQL2005 and SMO.

Thanks for taking time to read this.

Chuck Turner

Mapping a user to a login

I have created a login and a user on a database using SMO. I am having trouble mapping a database to the login. When you create a login from the GUI using SQL Server Management Studio Express CTP the user is automatically created and the database is mapped to this login from the user mapping tab. When I create the login programmatically this is not the case so I need to create the user. How do I map the database to the login. I set the default database property on the login but this does not do it.

Thanks,

Michael Gustafson

This is the pattern you would normally follow (this is for a windows user, but it works the same for a SQL user):

Login l = new Login(svr, @."REDMOND\mwories");
l.LoginType = LoginType.WindowsUser;
l.Create();

Database db = svr.Databases["AdventureWorks"];

User u = new User(db, @."REDMOND\mwories");
u.Login = l.Name;
u.UserType = UserType.SqlLogin;
u.Create();

This maps the database user to the login as you create the login.

|||

what property of the Login is used to show what databases are mapped?

also is there a way with SMO to check and see if a password is blank? Granted the odds of that are extemely slimmer with 2005.

|||

I found the DatabaseMapping() class and was able to find which logins were missing mappings. Im still at a lost at how to test for blank passwords using SQL2005 and SMO.

Thanks for taking time to read this.

Chuck Turner

mapped drive and backups

I mapped a drive to a different server, which has space, then tried to use
Ent. Manager to create a backup job to the mapped drive. Only, the mapped
isn't there only the local drives but the mapped appears locally to the OS.
I forgot something yes?
Thanks
HowTo: Backup to UNC name using Database Maintenance Wizard
http://support.microsoft.com/kb/555128
Geoff N. Hiten
Senior Database Administrator
Microsoft SQL Server MVP
"BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote in
message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
>I mapped a drive to a different server, which has space, then tried to use
> Ent. Manager to create a backup job to the mapped drive. Only, the mapped
> isn't there only the local drives but the mapped appears locally to the
> OS.
> I forgot something yes?
> Thanks
|||Hi
EM will not see mapped drives. Use T-SQL and backup to the UNC path
\\servername\sharename\file.bak.
Make sure that the SQL Server account is a domain account and has
permissions on the share.
Regards
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote in
message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
>I mapped a drive to a different server, which has space, then tried to use
> Ent. Manager to create a backup job to the mapped drive. Only, the mapped
> isn't there only the local drives but the mapped appears locally to the
> OS.
> I forgot something yes?
> Thanks
|||This actually worked.
EXEC sp_addumpdevice 'disk', 'device_name',
'\\filer_name\share_name\path\file_name.ext'
"Mike Epprecht (SQL MVP)" wrote:

> Hi
> EM will not see mapped drives. Use T-SQL and backup to the UNC path
> \\servername\sharename\file.bak.
> Make sure that the SQL Server account is a domain account and has
> permissions on the share.
> Regards
> --
> Mike Epprecht, Microsoft SQL Server MVP
> Zurich, Switzerland
> IM: mike@.epprecht.net
> MVP Program: http://www.microsoft.com/mvp
> Blog: http://www.msmvps.com/epprecht/
> "BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote in
> message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
>
>

mapped drive and backups

I mapped a drive to a different server, which has space, then tried to use
Ent. Manager to create a backup job to the mapped drive. Only, the mapped
isn't there only the local drives but the mapped appears locally to the OS.
I forgot something yes?
ThanksHowTo: Backup to UNC name using Database Maintenance Wizard
http://support.microsoft.com/kb/555128
Geoff N. Hiten
Senior Database Administrator
Microsoft SQL Server MVP
"BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote in
message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
>I mapped a drive to a different server, which has space, then tried to use
> Ent. Manager to create a backup job to the mapped drive. Only, the mapped
> isn't there only the local drives but the mapped appears locally to the
> OS.
> I forgot something yes?
> Thanks|||Hi
EM will not see mapped drives. Use T-SQL and backup to the UNC path
\\servername\sharename\file.bak.
Make sure that the SQL Server account is a domain account and has
permissions on the share.
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote in
message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
>I mapped a drive to a different server, which has space, then tried to use
> Ent. Manager to create a backup job to the mapped drive. Only, the mapped
> isn't there only the local drives but the mapped appears locally to the
> OS.
> I forgot something yes?
> Thanks|||This actually worked.
EXEC sp_addumpdevice 'disk', 'device_name',
'\\filer_name\share_name\path\file_name.ext'
"Mike Epprecht (SQL MVP)" wrote:
> Hi
> EM will not see mapped drives. Use T-SQL and backup to the UNC path
> \\servername\sharename\file.bak.
> Make sure that the SQL Server account is a domain account and has
> permissions on the share.
> Regards
> --
> Mike Epprecht, Microsoft SQL Server MVP
> Zurich, Switzerland
> IM: mike@.epprecht.net
> MVP Program: http://www.microsoft.com/mvp
> Blog: http://www.msmvps.com/epprecht/
> "BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote in
> message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
> >I mapped a drive to a different server, which has space, then tried to use
> > Ent. Manager to create a backup job to the mapped drive. Only, the mapped
> > isn't there only the local drives but the mapped appears locally to the
> > OS.
> > I forgot something yes?
> > Thanks
>
>sql

mapped drive and backups

I mapped a drive to a different server, which has space, then tried to use
Ent. Manager to create a backup job to the mapped drive. Only, the mapped
isn't there only the local drives but the mapped appears locally to the OS.
I forgot something yes?
ThanksHowTo: Backup to UNC name using Database Maintenance Wizard
http://support.microsoft.com/kb/555128
Geoff N. Hiten
Senior Database Administrator
Microsoft SQL Server MVP
"BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote in
message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
>I mapped a drive to a different server, which has space, then tried to use
> Ent. Manager to create a backup job to the mapped drive. Only, the mapped
> isn't there only the local drives but the mapped appears locally to the
> OS.
> I forgot something yes?
> Thanks|||Hi
EM will not see mapped drives. Use T-SQL and backup to the UNC path
\\servername\sharename\file.bak.
Make sure that the SQL Server account is a domain account and has
permissions on the share.
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote in
message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
>I mapped a drive to a different server, which has space, then tried to use
> Ent. Manager to create a backup job to the mapped drive. Only, the mapped
> isn't there only the local drives but the mapped appears locally to the
> OS.
> I forgot something yes?
> Thanks|||This actually worked.
EXEC sp_addumpdevice 'disk', 'device_name',
'\\filer_name\share_name\path\file_name.ext'
"Mike Epprecht (SQL MVP)" wrote:

> Hi
> EM will not see mapped drives. Use T-SQL and backup to the UNC path
> \\servername\sharename\file.bak.
> Make sure that the SQL Server account is a domain account and has
> permissions on the share.
> Regards
> --
> Mike Epprecht, Microsoft SQL Server MVP
> Zurich, Switzerland
> IM: mike@.epprecht.net
> MVP Program: http://www.microsoft.com/mvp
> Blog: http://www.msmvps.com/epprecht/
> "BrianMultiLanguage" <BrianMultiLanguage@.discussions.microsoft.com> wrote
in
> message news:C8DDEE27-FEC9-4FA7-BEFD-03048AA5A774@.microsoft.com...
>
>

Monday, March 26, 2012

Many-to-Many Relationship?

I cannot get my head round how to create a database where two of the tables
require a many-to-many relationship.
I posted on another group and was advised to make a third joining table, but
I don't really understand how this works.
Can anyone help?
Basically I have a table of Individuals, and a table of Businesses. A
business can be related to many individuals, and an individual to many
businesses.
ThanksKeith
Did you read the link which David posted here?
"Keith" <@..> wrote in message news:esRuNtWFEHA.2976@.TK2MSFTNGP10.phx.gbl...
> I cannot get my head round how to create a database where two of the
tables
> require a many-to-many relationship.
> I posted on another group and was advised to make a third joining table,
but
> I don't really understand how this works.
> Can anyone help?
> Basically I have a table of Individuals, and a table of Businesses. A
> business can be related to many individuals, and an individual to many
> businesses.
> Thanks
>|||Keith wrote:
> I cannot get my head round how to create a database where two of the
> tables require a many-to-many relationship.
> I posted on another group and was advised to make a third joining
> table, but I don't really understand how this works.
> Can anyone help?
> Basically I have a table of Individuals, and a table of Businesses. A
> business can be related to many individuals, and an individual to many
> businesses.
Yes, a junction table is needed. From BOL - it is a table that establishes a
relationship between other tables. The junction table contains foreign keys
referencing the tables that form the relationship. For example, an
OrderParts junction table can show what parts shipped with each order by
having foreign keys to an Orders table and a Parts table. In your case:
Business
--
PK ID_Business int
(...)
Individuals
--
PK ID_Individual int
(...)
BusinessIndividuals
--
PK ID_Business int
PK ID_Individual int
Business.ID_Business -- BusinessIndividuals.ID_Business
Individuals.ID_Individual -- BusinessIndividuals.ID_Individual
sincerely,
--
Sebastian K. Zaklada
Skilled Software
http://www.skilledsoftware.com
This posting is provided "AS IS" with no warranties, and confers no rights.|||Hi Keith
Many to Many relationships takes a bit of effort to get
your head around.
Anyways here is my take on it.
You have a table called student, and a table called class.
A Student can take may classes, and a class can have many
students.
So in the Student Table you would have something like
Keith SQL Beginner Course
Keith SQL Intermediate Course
Julie SQL Beginner Course
Matt ASP Beginner Course
However in the Class table you would have
SQL Beginner Course Keith
SQL Beginner Course Julie
SQL Intermediate Course Keith
ASP Beginner Course Matt
Now this is bad because you are copying data (third normal
form), in the Student table it would be better to have
each student entered once, and in classes, its better to
enter a classes only once.
Here is where the third table comes in.
So out Student Table we would have
Keith
Julie
Matt
and in the Classes table we would have
SQL Beginner Course
SQL Intermediate Course
ASP Beginner Course
Now you would have a link table
Keith SQL Beginner Course
Keith SQL Intermediate Course
Julie SQL Beginner Course
Matt ASP Beginner Course
Anyway the example is a very simple one. There are tons of
examples out on the net.
Enjoy
J

>--Original Message--
>Keith
>Did you read the link which David posted here?
>"Keith" <@..> wrote in message
news:esRuNtWFEHA.2976@.TK2MSFTNGP10.phx.gbl...
where two of the
>tables
third joining table,
>but
Businesses. A
individual to many
>
>.
>|||Thanks everyone
I have just got my head round this now (or at least have got it working -
still not sure I understand it :-))
"Keith" <@..> wrote in message news:esRuNtWFEHA.2976@.TK2MSFTNGP10.phx.gbl...
> I cannot get my head round how to create a database where two of the
tables
> require a many-to-many relationship.
> I posted on another group and was advised to make a third joining table,
but
> I don't really understand how this works.
> Can anyone help?
> Basically I have a table of Individuals, and a table of Businesses. A
> business can be related to many individuals, and an individual to many
> businesses.
> Thanks
>

Friday, March 23, 2012

Many Year Payments

I need to create a stored procedure that will show all clients that have made payments every year for a number of years (3) or (5). My tables are simple but I have no clue how to ask the SQL question, mostly because it is each and every year.

tblClients ClientID (pk)
tblOrders OrderID (pk) ClientID (fk)
tblOrderPayments PaymentID (pk) OrderID (fk)

Thanks for any help,Depends on how you would like the output. Also: In your tables there's no date. And is not possible for one payment to apply to many orders?

And before I start: You do not 'ask SQL questions', rather you 'construct SQL queries'. And lastly: Always use singular names for your tables, e.g. Client and not Clients, since all tables can hold multiple records all of your table names end up being plural and so the plural becomes redundant.

Anyway. I'll use some common sense to fill in the gaps in your question: I'll assume the payment date rather than the order date is the important one, and I'll assume therefore there's a date on the payment table. Also I'll assume for a moment that your DB design is correct although I doubt it, since it's almost certainly the case that several orders can be paid with one order. I do not know of businesses that does not allow you to pay off several orders in one payment.

So your question as stated is like follows: You want a list of all clients that's made at least one payment a year for the last X years. Is that right? Sounds like a weird question for a business to ask...

If this is indeed what they want, it's a rather interesting SQL problem. I'll give you a few solutions just for my own amusement, even though I suspect they'll be useless for you because your DB design is wrong and you probably misunderstood the needs of your management.

Here's my first attempt:


set @.EndYear = 2004

select CustomerID,
CustomerName
from Customer C
where CustomerID in (select distinct CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and datepart(year, P.Date) = @.EndYear)
and CustomerID in (select distinct CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and datepart(year, P.Date) = @.EndYear - 1)
and CustomerID in (select distinct CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and datepart(year, P.Date) = @.EndYear - 2)


It will work for the 3 year case, but sucks bigtime. If you have a small database (less than 1 million orders) then this will be the way to go since it's easy to understand and you probably won't notice and bad performance. If one of my guys wrote this query for me I'd not be impressed though.
It sucks because:
- Only works for a set number of years. You'll have to write 2 of them to cater for 3 or 5 year queries
- Uses IN with large sets, several times. Bad for performance.
It's not all bad, though. It's cool because:
- It's simple to understand for anyone out there
- Does not use cursors or temp tables

Here's a slightly better one:


set @.EndYear = 2004

select CustomerID,
CustomerName
from Customer C
join (select distinct CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and datepart(year, P.Date) = @.EndYear) as Y1
on C.CustomerID = Y1.CustomerID
join (select distinct CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and datepart(year, P.Date) = @.EndYear - 1) as Y2
on C.CustomerID = Y2.CustomerID
join (select distinct CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and datepart(year, P.Date) = @.EndYear - 2) as Y3
on C.CustomerID = Y2.CustomerID


The same as the first one, but without the expensive IN statements. Still uses a lot of DISTINCTs though, and you still have to write 2 queries for the 3 and 5 year cases. Let's extrapolate this query to one that will work for any number of years from 1 to 5:

set @.EndYear = 2004
set @.NoYears = 4

select CustomerID,
CustomerName
from Customer C
join (select CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and datepart(year, P.Date) = @.EndYear) as Y1
on C.CustomerID = Y1.CustomerID
join (select CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and (datepart(year, P.Date) = @.EndYear - 1
or @.NoYears < 2)) as Y2
on C.CustomerID = Y2.CustomerID
join (select CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and (datepart(year, P.Date) = @.EndYear - 2
or @.NoYears < 3)) as Y3
on C.CustomerID = Y3.CustomerID
join (select CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and (datepart(year, P.Date) = @.EndYear - 3
or @.NoYears < 4)) as Y4
on C.CustomerID = Y4.CustomerID
join (select CustomerID
from Order O
join Payment P
on O.OrderID = P.OrderID
and (datepart(year, P.Date) = @.EndYear - 4
or @.NoYears < 5)) as Y5
on C.CustomerID = Y5.CustomerID


Whoa! That's a good one eh? Again, it will work, but I'll probably get blasted by the community here for even thinking about posting **** like that. The query will work for any number of years to check from 1 to 5, so you can do your 3 and 5 year queries on it just fine without the need to write 2 seperate queries.

Ok, enough of that. Now let's try another approach: Instead of doing it all in one SQL statement, we'll create a temp table with all CustomerIDs in it. Then we'll iterate through the years we want to check, and remove all customers that did not make a payment in each year. So in the end we're left with only the customers that made payments in all the years checked.


set @.EndYear = 2004
set @.NoYears = 4
set @.I = 0

select CustomerID
into #Temp
from Customer

while (@.I < @.NoYears)
begin

delete T
from Order O
join Payment P
on O.OrderID = P.OrderID
and datepart(year, P.Date) = @.EndYear - @.I
right join #Temp T
on O.CustomerID = T.CustomerID
where P.OrderID is null

set @.I = @.I + 1

end

select C.CustomerID,
C.CustomerName
from Customer C
join #Temp T
on C.CustomerID = T.CustomerID


This is also a nice and simple solution, and will probably give you fairly decent performance. The good thing about it is that it will work for ANY number of years, although the more years you do the longer it will take. It's also sexy because it uses a RIGHT JOIN, my favourite type of join. :)

I'm going to stop here, but there are almost certainly more elegant solutions out there. I can imagine one where you use a helper table for all the years you want to check, and then do a distinct and a having count on a simple join in a sub-query - it will work in a single query and will be able to do an arbitraty number of years. I'll challenge anyone else out there to give it a shot and I'll post it in a day or two.|||Hi i think i found a nice solution for you :).

Data: i used the Northwind database for testing, i created a payments table containing customerid, orderid and paymentdate and populated it with testdata using the orders table.


DECLARE @.YearSpan int
SET @.YearSpan = 3

SELECT
YearsOfPayment.CustomerID,
Sum(FirstYear) As FirstYearPayments,
Sum(SecondYear) As SecondYearPayments,
Sum(ThirdYear) As ThirdYearPayments,
Sum(FourthYear) As FourthYearPayments,
Sum(FifthYear) As FifthYearPayments

FROM
(
Select
Customers.CustomerID,
(
Case When Payments.Paymentdate Between '01/01/1996' And '12/31/1996' Then 1 Else 0 End
) AS FirstYear,
(
Case When Payments.Paymentdate Between '01/01/1997' And '12/31/1997' Then 1 Else 0 End
) AS SecondYear,
(
Case When Payments.Paymentdate Between '01/01/1998' And '12/31/1998' Then 1 Else 0 End
) AS ThirdYear,
(
Case When Payments.Paymentdate Between '01/01/1999' And '12/31/1999' Then 1 Else 0 End
) AS FourthYear,
(
Case When Payments.Paymentdate Between '01/01/2000' And '12/31/2000' Then 1 Else 0 End
) AS FifthYear
From
Customers
Inner Join Payments On Payments.CustomerID = Customers.CustomerID
) AS YearsOfPayment

GROUP BY
YearsOfPayment.CustomerID

HAVING
SUM(FirstYear) > 0
AND SUM(SecondYear) > 0
AND SUM(ThirdYear) > 0
AND SUM(FourthYear) > (CASE WHEN @.YearSpan > 3 THEN 0 ELSE -1 END)
AND SUM(FifthYear) > (CASE WHEN @.YearSpan > 4 THEN 0 ELSE -1 END)

Todo:
You have to replace the hardcoded dates with a @.FirstYear Parameter and modify the WHEN
clauses accordingly ;)

Good things here:
- if you need to check for more years, just modify it to return an additional CASE column
- performance won't change with the number of years checked (= the number of CASES you return since no subselects are involved

Further Tuning:
- to speed it up a even little more you can create and index on the paymentdate and put a WHERE statement in the "inner" SELECT to only check the years between @.firstdate and @.firstdate + @.yearspan

- Moon|||:: And before I start: You do not 'ask SQL questions', rather you 'construct SQL queries'.

... and before you post again, you should rethink your arrogant attidue and stop correcting someone since most people will find this very offensive.

:: And lastly: Always use singular names for your tables, e.g. Client and not Clients,
:: since all tables can hold multiple records all of your table names end up being plural
:: and so the plural becomes redundant.

... from OO point of view where you think of collections and items, a table is a collection and holding items (records) and therefore it is 100% valid to use plural.|||Moon's solution is novel, but only works for up to a certain number of years. Here's my solution that will work for an arbitrary number of years, with only one query and no temp tables or cursors:


set @.LastYear = 2004
set @.NumYears = 5

select D.CustomerId,
C.[Name]
from Customer C
join (select distinct O.CustomerId,
datepart(year, P.[Date]) as PaidYear
from [Order] O
join Payment P
on O.OrderId = P.OrderId
and datepart(year, P.[Date]) <= @.LastYear
and datepart(year, P.[Date]) > @.LastYear - @.NumYears) as D
on C.CustomerID = D.CustomerID
group by D.CustomerID,
C.[Name]
having count(*) = @.NumYears

Who's your daddy! $5 for anyone with a more elegant solution.
And if I come off as arrogant, feel free to skip my comments and just look at my code.|||Relational databases are NOT OO and therefore your plural argument does not hold.

From IDEF1X

December 21, 1993

Section: 3.1.2 Entity Syntax

...The entity name is a noun phrase that describes the set of things the entity
represents. The noun phrase is in singular form, not plural. Abbreviations and
acronyms are permitted, however, the entity name must be meaningful and consistent
throughout the model...

IE and other data modeling standards organisations agree with the above statement. Also I was taught the same thing at University in my first year. I was under the impression that this is common knowledge.|||*points at the year, cough, :P*

well as long as Microsoft uses the plural convention in their own databases: master, Northwind, Pubs, ... i imply that there is a point in using plural for his own database objects as well ;)

Also i didn't say that using singular is a complete NoNo, just that using plural is not forbidden but valid to use.

PS: University != real life ;). I also was taught a few things in university such as C++ that are invalid by now - since when i learned C++, it was still a draft, without namespaces, and whatnot and if i would need to code in C++ today it would be a nearly a full restart.|||Yeah this one is nice :)

And sorry my yesterdays comment was rude as well :S.|||Like I said: If you start naming tables with plurals, then in the end ALL your tables (or at least a very large percentage) will end up being plural, and so the 's' at the end of every table becomes just another letter you have to type in every line of SQL code and serves no purpose whatsoever. Remember that 99% of all tables will have more than one row and therefore should in your argument be plural in the naming.

And who ever said MS wrote good code, eh?

The date may be 10 years old, but please feel free to provide me with a more recent reference from an SQL standards authority that says 'Yea, we made a mistake in '93, go ahead and use plurals now'

I disagree with you. There's no good reason to use plurals. Using plurals is a nono. Except if you're MS in which case you are expected to write bad code and thus are allowed plurals in table names.

Many to Many to Many

In my DB I am working on, I have 4 many-to-many relationships.
However, I now need to join two of those together to create a
many-to-many-to-many relationship.
Is this possible? I have tried what i thought was correct (creating another
join table) and got erroneous results to say the least.
better to post ddl or give more real details
>--Original Message--
>In my DB I am working on, I have 4 many-to-many
relationships.
>However, I now need to join two of those together to
create a
>many-to-many-to-many relationship.
>Is this possible? I have tried what i thought was
correct (creating another
>join table) and got erroneous results to say the least.
>
>.
>
sql

Many to Many to Many

In my DB I am working on, I have 4 many-to-many relationships.
However, I now need to join two of those together to create a
many-to-many-to-many relationship.
Is this possible? I have tried what i thought was correct (creating another
join table) and got erroneous results to say the least.better to post ddl or give more real details
>--Original Message--
>In my DB I am working on, I have 4 many-to-many
relationships.
>However, I now need to join two of those together to
create a
>many-to-many-to-many relationship.
>Is this possible? I have tried what i thought was
correct (creating another
>join table) and got erroneous results to say the least.
>
>.
>

many to many dimension

Hi all,

Is there a way to create a dimension property based on fact table used to connect a dimension to another measure group. I found a example where such an attibute is used for a measure expression(http://msdn2.microsoft.com/en-us/library/ms345139.aspx) but I really need a property that I could display to the end user.

thx

Fred

Hi again...just to be a bit more clear on what I'm trying to do...if you look at the example I mentioned....how could the value of the 'ownership' be displayed instead of used in a calculation?

Fred

|||Well, in this example, the user could browse the value of the 'ownership' measure in the intermediate measure group, for a given selection of Store and Organization. If that's not adequate, you could define a fact dimension for the intermediate measure group fact table, and add 'ownership' as an attribute (with the appropriate type).|||

Thanks Deepak! The fact dimension will do the trick

Fred

Wednesday, March 21, 2012

Manualy Create IDENTITY Column inside ControlFlow

Dear Friends... I'm having a problem...

I want to manually create the identity column for a table...

I have some dataflws, and in each dataflow I insert values in this table...

I need to start the controlflow in a SQL task to get the last ID and save it in a global variable with name D_INST_IDENTITY.

And in each dataflow I have a script component transform, to get the ID... using a local variable COUNTER! and for each row I increment this value...

Until this step there is no problem... the problem starts here...:

I need to refresh the global variable in the final of each dataflow in order that in the next sequence dataflow I have D_INST_IDENTITY refreshed......

D_INST_IDENTITY = D_INST_IDENTITY + COUNTER

How can I do it? I have a RowCount transform next the script component, but generates errors...

What do you think I can do it?
Thanks!!

Can you not just use an Execute SQL Task to get the current max value in the target table and store is in a variable?

-Jamie

|||

I can do it... but I have some dataflows, and each dataflow insert in this same table... so, In the finaly of each sequence dataflow, i need tro refresh the identity value... in the begin of each dataflow I need to get this IDENTITY...

Example

1. Dataflow

Return the Initial value of IDENTITY_variable from SQL Task

Refresh Identity_variable

2. Dataflow

Read Identity_variable

Refresh Identity_variable

3. Dataflow

Read Identity_variable

Refresh Identity_variable

|||

And In the SQL Task I use:

SELECT MAX(INST_ID)+1 FROM Instrumento

But if teh table is empty returns me an error... Is this statment that is usually used?

|||

PedroCGD wrote:

I can do it... but I have some dataflows, and each dataflow insert in this same table... so, In the finaly of each sequence dataflow, i need tro refresh the identity value... in the begin of each dataflow I need to get this IDENTITY...

You don't do it IN the data-flow, you do it BEFORE the data-flow

PedroCGD wrote:

Example

1. Dataflow

Return the Initial value of IDENTITY_variable from SQL Task

Refresh Identity_variable

2. Dataflow

Read Identity_variable

Refresh Identity_variable

3. Dataflow

Read Identity_variable

Refresh Identity_variable

So you put an Execute SQL Task before each data-flow. Is there a problem with doing that?

(There are actually some cleverer ways of doing it but for now - let's keep it simple.)

-Jamie

|||

PedroCGD wrote:

And In the SQL Task I use:

SELECT MAX(INST_ID)+1 FROM Instrumento

But if teh table is empty returns me an error... Is this statment that is usually used?

It really helps if, when you get an error, you tell us the error emssage.

I'm pretty sure I can guess what it is though. Try this:

SELECT ISNULL(MAX(INST_ID), 0) +1 FROM Instrumento

-Jamie

|||

Dear Jamie,

In order I have teh best performance, I avoid to use multiple SQL tasks. Imagin that I have 20 dataflows? Why I need to execute a query in database for each dataflow, if I have a counter to automatically give me the IDENTITY column? Do you think is more consistent using SQL Task?

I have found the solution to read a global variavel, and change it inside the script component transform...

I initialize in the Script Component properties the variable INST_IDENTITY as ReadWritevariables, and in order to avoid errors inside the PreExecute method when I read the value, I changed the code as you can see:

Imports System

Imports System.Data

Imports System.Math

Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper

Imports Microsoft.SqlServer.Dts.Runtime.Wrapper

Public Class ScriptMain

Inherits UserComponent

Dim counter As Integer

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

counter = counter + 1

Row.TesteKey = counter

End Sub

Public Overrides Sub PreExecute()

Dim vars As IDTSVariables90 = Nothing

Me.VariableDispenser.LockForRead("User::INST_IDENTITY")

Me.VariableDispenser.GetVariables(vars)

counter = CType(vars(0).Value, Integer)

vars.Unlock()

MyBase.PreExecute()

End Sub

Public Overrides Sub PostExecute()

Me.ReadWriteVariables("INST_IDENTITY").Value = counter

MyBase.PostExecute()

End Sub

Protected Overrides Sub Finalize()

MyBase.Finalize()

End Sub

End Class

I was having errors if I read the value of a ReadWriteVariables inside the PreExcute Method, so, using VariableDispenser I can do it!!

Thanks!!

|||

PedroCGD wrote:

I have found the solution to read a global variavel, and change it inside the script component transform...

Cool! That is the "other way" that I alluded to earlier.

-Jamie

Monday, March 19, 2012

Manually Creating Membership Tables

Hi, I have limited control on the server I use. I can not create data bases but can add tables and edit the data base that has been created for me. So what I would like to do is be able to use a tool such as aspnet_regsql through SQL Server Express to add the tables that Membership would automatically create. Any help in pointing me in the right direction would be great

Thanks

howlinhuskie

Hi,

if you're able to execute sql statements on the server you could use the aspnet_regsql tool to create all the necessary sql statements:http://forums.asp.net/p/994473/1298256.aspx#1298256.

Grz, Kris.

|||

Thanks for looking in on me Kris. Can the aspnet_regsql tool be run using SQL Server Express or do you have to run it through the dos command line? Is there step by step instructions anywhere?

Any help would be great

howlinhuskie

|||

Hi,

indeed you have to run it from the command line.

Grz, Kris.

|||

Hi,

also take a look at this article:Create Membership tables in another database than the standard aspnetdb.mdf.

Grz, Kris.

|||

Thanks again Kris, I do not have the permissions on the server to access the command line.

Mike

|||

howlinhuskie:

I do not have the permissions on the server to access the command line.

And from your own developer pc? If you already have a database and know its name you can use the command line tool from your dev pc, generate the sql statements and later on use Query Analyzer or SQL Server Management Studio to run those queries on the remote database server. You do have permissions to run sql statements on your SQL Server right? In case not you could still generate it and have a DBA install it.

Grz, Kris.

Monday, March 12, 2012

Manual creation of DSNs

I'm trying to adapt the code in the article "How to Create ODBC DSN on
multiple SQL server Machines"
(http://www.databasejournal.com/feat...cle.php/2238221) to
create a script that will automate the creation of SQL Server DSNs.
The article's code writes the DSN to the appropriate location in the
registry--HKLM\SOFTWARE\ODBC\ODBC.INI\<DSN Name>. However, the code doesn't
seem to address how to write the SQL UserID and Password. It appears that
the SQL driver doesn't store that information in the registry.
Any suggestions on how to proceed? Thanks!"Stan" <NoSpam@.I> wrote in message
news:#qSSJ77zDHA.1740@.TK2MSFTNGP09.phx.gbl...
quote:

> I'm trying to adapt the code in the article "How to Create ODBC DSN on
> multiple SQL server Machines"
> (http://www.databasejournal.com/feat...cle.php/2238221) to
> create a script that will automate the creation of SQL Server DSNs.
> The article's code writes the DSN to the appropriate location in the
> registry--HKLM\SOFTWARE\ODBC\ODBC.INI\<DSN Name>. However, the code

doesn't
quote:

> seem to address how to write the SQL UserID and Password. It appears that
> the SQL driver doesn't store that information in the registry.
> Any suggestions on how to proceed? Thanks!

This article was designed to use Trusted Connections -- reference the
TrustedConnection = "yes" entry.
Usually it's not a good idea to embed the user name and password with the
DSN entry. Consider using DSN-less entries if you are writing a VB
application to access SQL Server. Here is a starting point for the
connection strings: http://www.able-consulting.com/ADO_Conn.htm
Steve

manipulating binary data

To all,
I have a binary data type in my database. It is an array of doubles. I
would like to create a DTS package that can change this binary data type int
o
an array so I can retrieve one of the double values at a specific index.
Is there any way to do this?
How can I cast the binary data type in the database as an array of doubles?
Thanks in advance,
GloriaThere is no such thing in SQL as an array. So, to understand this clearly,
the data yoou have in the database, even though it was an array of doubles i
n
your client code before you sent it there, in the database it's just a byte
stream, or a long string of bytes...
To convert it to individual values, will most easily be done using client
side code in some programming language.
If the "array" was a delimited list of doubles represesnted as text, then it
miht be possible to do this parsing and separating using some SQL Code, but
even tis is medium to hard. If the data in your binary data column is the
actual binary byte stream generated by some client side code language to
represent an array of IEEE Double precision floats, then there's probably no
hope of parsing those individual values out in SQL Code...
"Gloria" wrote:

> To all,
> I have a binary data type in my database. It is an array of doubles. I
> would like to create a DTS package that can change this binary data type i
nto
> an array so I can retrieve one of the double values at a specific index.
> Is there any way to do this?
> How can I cast the binary data type in the database as an array of doubles
?
> Thanks in advance,
> Gloria|||There are no arrays in SQL, nor do we use it for bit level
manipulations. Indexes in SQL are not exposed toi the programmer; in
fact they are a implementattion method that not all products use
(Teradata is based on hashing; Nucleus uses bit vectors, etc.).
You have missed the point of SQL and have returned to a C style file
system in your mental model. And there is nothing wrong with a file or
programming at the machine level for certain problems. Databases are
not one of those problems.

Friday, March 9, 2012

managing users

Hello,

Has anyone ever come across a reason why someone would manually create a
user table incl. permission flags and not use the inbuilt user/roles
provided by that database? The only reason that stands out for me is to
make the database that bit more portable?

Thanks,
Craig.Craig, it would depend on exactly how the table in question is
constructed and used but some applications provide application based
security. The application might connect to the database using one ID
that is the database owner or has both datareader and datawriter but
via the application limit what end-users can do.

HTH -- Mark D Powell --|||Craig H. (spam@.thehurley.com) writes:
> Has anyone ever come across a reason why someone would manually create a
> user table incl. permission flags and not use the inbuilt user/roles
> provided by that database? The only reason that stands out for me is to
> make the database that bit more portable?

The database-level can be a bit heavy-handed. In our application, our
security scheme on SQL Server is dead simple. All users are added to
a group, and that group is granted execute access on all stored procedures
and select access on most tables.

Then our database includes tables to control access to application
functions, and also access to which accounts and customers a user may
see.

True, our system started its life in the days of 4.x when the permission
system in SQL Server was far less sophistcated than today. But since
our securable entities are not SQL Server entities, I can't see how
SQL Server could help us, even if we were to make a complete restart.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Monday, February 20, 2012

Management Studio will not allow me to create a view

Hi there,
When I run the following query I get the correct result.
select * from Inventory As I Full Outer Join Publisher As P on
I.ID=P.InventoryID
However, when I try to create a view with the same select statement I get
the following error:
Msg 4506, Level 16, State 1, Procedure InventoyPublisherView, Line 2
Column names in each view or function must be unique. Column name 'ID' in
view or function 'InventoyPublisherView' is specified more than once.
The CREATE VIEW statement I'm using is:
CREATE VIEW InventoyPublisherView AS
(SELECT * FROM Inventory AS I FULL OUTER JOIN Publisher AS P ON
I.ID=P.InventoryID)
Many thanks in advance for the help. Very much appreciatedName the columns in the SELECT statement. Both tables has a column named ID,
and the view cannot
have two columns with the same name.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Chris L" <ChrisL@.discussions.microsoft.com> wrote in message
news:0C10F52D-2845-4F6B-92B7-648A5D1FBC50@.microsoft.com...
> Hi there,
> When I run the following query I get the correct result.
> select * from Inventory As I Full Outer Join Publisher As P on
> I.ID=P.InventoryID
> However, when I try to create a view with the same select statement I get
> the following error:
> Msg 4506, Level 16, State 1, Procedure InventoyPublisherView, Line 2
> Column names in each view or function must be unique. Column name 'ID' in
> view or function 'InventoyPublisherView' is specified more than once.
> The CREATE VIEW statement I'm using is:
> CREATE VIEW InventoyPublisherView AS
> (SELECT * FROM Inventory AS I FULL OUTER JOIN Publisher AS P ON
> I.ID=P.InventoryID)
> Many thanks in advance for the help. Very much appreciated
>|||"Chris L" <ChrisL@.discussions.microsoft.com> wrote in message
news:0C10F52D-2845-4F6B-92B7-648A5D1FBC50@.microsoft.com...
> Hi there,
> When I run the following query I get the correct result.
> select * from Inventory As I Full Outer Join Publisher As P on
> I.ID=P.InventoryID
> However, when I try to create a view with the same select statement I get
> the following error:
> Msg 4506, Level 16, State 1, Procedure InventoyPublisherView, Line 2
> Column names in each view or function must be unique. Column name 'ID' in
> view or function 'InventoyPublisherView' is specified more than once.
> The CREATE VIEW statement I'm using is:
> CREATE VIEW InventoyPublisherView AS
> (SELECT * FROM Inventory AS I FULL OUTER JOIN Publisher AS P ON
> I.ID=P.InventoryID)
> Many thanks in advance for the help. Very much appreciated
It's because you have a column named ID in both tables.
If you didn't use Select * (and you should not) you wouldn't have the
problem. Name the columns.
Besides, why would you select I.ID and P.InventoryID in the query since they
have the same value.|||chris,
use column names in the select list instead of the asterisk:
select i.id, i.col2, i.col3, p.col1, p.col2, etc..
from Inventory As I Full Outer Join Publisher As P on I.ID=P.InventoryID
dean
"Chris L" <ChrisL@.discussions.microsoft.com> wrote in message
news:0C10F52D-2845-4F6B-92B7-648A5D1FBC50@.microsoft.com...
> Hi there,
> When I run the following query I get the correct result.
> select * from Inventory As I Full Outer Join Publisher As P on
> I.ID=P.InventoryID
> However, when I try to create a view with the same select statement I get
> the following error:
> Msg 4506, Level 16, State 1, Procedure InventoyPublisherView, Line 2
> Column names in each view or function must be unique. Column name 'ID' in
> view or function 'InventoyPublisherView' is specified more than once.
> The CREATE VIEW statement I'm using is:
> CREATE VIEW InventoyPublisherView AS
> (SELECT * FROM Inventory AS I FULL OUTER JOIN Publisher AS P ON
> I.ID=P.InventoryID)
> Many thanks in advance for the help. Very much appreciated
>|||Thank you very much, Tibor. I am following exercises from a Wrox book. I
think I will have to shoot the author for writing incorrect code in his
examples.
"Tibor Karaszi" wrote:

> Name the columns in the SELECT statement. Both tables has a column named I
D, and the view cannot
> have two columns with the same name.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Chris L" <ChrisL@.discussions.microsoft.com> wrote in message
> news:0C10F52D-2845-4F6B-92B7-648A5D1FBC50@.microsoft.com...
>|||Thank you very much Raymond. The speed of all of your replies (from all of
you guys) is very reassuring for a total beginner like myself. Fantastic job
.
thanks
"Raymond D'Anjou" wrote:

> "Chris L" <ChrisL@.discussions.microsoft.com> wrote in message
> news:0C10F52D-2845-4F6B-92B7-648A5D1FBC50@.microsoft.com...
> It's because you have a column named ID in both tables.
> If you didn't use Select * (and you should not) you wouldn't have the
> problem. Name the columns.
> Besides, why would you select I.ID and P.InventoryID in the query since th
ey
> have the same value.
>
>|||Thanks Dean. Much appreciated. Like I mentioned in the post above the author
of the book I′m following put incorrect code in his examples. Luckily, ther
e
are great people out there to come to the resuce. Cheers
"Dean" wrote:

> chris,
> use column names in the select list instead of the asterisk:
> select i.id, i.col2, i.col3, p.col1, p.col2, etc..
> from Inventory As I Full Outer Join Publisher As P on I.ID=P.InventoryID
> dean
> "Chris L" <ChrisL@.discussions.microsoft.com> wrote in message
> news:0C10F52D-2845-4F6B-92B7-648A5D1FBC50@.microsoft.com...
>
>|||"Chris L" <ChrisL@.discussions.microsoft.com> wrote in message
news:00CEBECA-7183-436E-BB8A-CA36F3388B23@.microsoft.com...
> Thank you very much, Tibor. I am following exercises from a Wrox book. I
> think I will have to shoot the author for writing incorrect code in his
> examples.
This must be why WROX went bankrupt.
All their authors were shot. :-)

Management Studio Solution user-defined project folders?

When you create a Solution/Project in Management Studio, it creates 3 folders for you - Connections, Queries, Miscellaneous.

It does not appear that there exists the ability to create your own set of folders, either under the project or any of the 3 provided folders. Does anyone know of a way to do this?

If you are working on a project that has hundreds, perhaps thousands of stored procedures, views, etc., there currently seems to be no way to organize them. If this is true, this is an incredible MS oversight!

Thanks!

You have found the growing pain problems with the tool...maybe future versions will correct it.|||I would hope so! Since Mgmt Studio is based upon the Visual Studio shell, it doesn't seem like it would be that hard. In fact, I would think they would have had to specifically disable that functionality in this version, it seems so basic and fundamental to an IDE.|||

This is one of our most requested features. You can track the progress of this at http://connect.microsoft.com.

Here is the direct link to the issue: https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=124787

Vote on it and let us know that it is important to you.

Paul A. Mestemaker II
Program Manager
Microsoft SQL Server
http://blogs.msdn.com/sqlrem/

|||

I already did. I also started my own post to this issue a long time ago.

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=127202

|||

Ahh, nice... even though your solution is not solved, can you mark the post as answered?

Management Studio Solution user-defined project folders?

When you create a Solution/Project in Management Studio, it creates 3 folders for you - Connections, Queries, Miscellaneous.

It does not appear that there exists the ability to create your own set of folders, either under the project or any of the 3 provided folders. Does anyone know of a way to do this?

If you are working on a project that has hundreds, perhaps thousands of stored procedures, views, etc., there currently seems to be no way to organize them. If this is true, this is an incredible MS oversight!

Thanks!

You have found the growing pain problems with the tool...maybe future versions will correct it.|||I would hope so! Since Mgmt Studio is based upon the Visual Studio shell, it doesn't seem like it would be that hard. In fact, I would think they would have had to specifically disable that functionality in this version, it seems so basic and fundamental to an IDE.|||

This is one of our most requested features. You can track the progress of this at http://connect.microsoft.com.

Here is the direct link to the issue: https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=124787

Vote on it and let us know that it is important to you.

Paul A. Mestemaker II
Program Manager
Microsoft SQL Server
http://blogs.msdn.com/sqlrem/

|||

I already did. I also started my own post to this issue a long time ago.

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=127202

|||

Ahh, nice... even though your solution is not solved, can you mark the post as answered?

|||I agree it is a huge MS oversight... fortunately theres a 3rd party solution available... Here's the link SQL Server Management Studio 2005 Project Plugin|||I agree it is a huge MS oversight... fortunately theres a 3rd party solution available... Here's the link SQL Server Management Studio 2005 Project Plugin

Management Studio scripts for SQL 2000

When I use the generate scripts task in Management Studio to create tables
for a SQL 2000 target, the scripts do not run in SQL 2000.
I selected SQL 2000 compatibility mode. The scripts work fine on a 2000 database
running under SQL 2005 but not on the real thing.
Is there a easy way to generate scripts that run on a SQL 2000 instance?
Darren (Darren@.nospam.nospam) writes:
> When I use the generate scripts task in Management Studio to create tables
> for a SQL 2000 target, the scripts do not run in SQL 2000.
Exactly why do they not run? Because they refer to sys.objects, of because
of some other problem? I just scripted a database on SQL 2005 for SQL 2000,
and the script ran on SQL 2000.
I seem to recall that that there was a problem with scripts for SQL 2000
referring to sys.objects in the RTM release ot SQL 2005, but this was
corrected in SP1. You can find SP1 of SQL 2005 at
http://www.microsoft.com/sql/sp1.mspx.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
|||Thanks. I wasn't aware of SP1.
Erland Sommarskog wrote:
> Darren (Darren@.nospam.nospam) writes:
> Exactly why do they not run? Because they refer to sys.objects, of because
> of some other problem? I just scripted a database on SQL 2005 for SQL 2000,
> and the script ran on SQL 2000.
> I seem to recall that that there was a problem with scripts for SQL 2000
> referring to sys.objects in the RTM release ot SQL 2005, but this was
> corrected in SP1. You can find SP1 of SQL 2005 at
> http://www.microsoft.com/sql/sp1.mspx.
>
>