Showing posts with label working. Show all posts
Showing posts with label working. Show all posts

Friday, March 30, 2012

Scripting a database

I just starting working for a new client this week. They have a custom install program written in VB.NET which installs the database to SQL Server 2000. Normally, when I want to create a SQL script to create a database, I would go into Enterprise Manager, right-click on a database, and select All Tasks | Generate SQL Script. However, that's not how this client works. Much to my horror, the CREATE DATABASE, CREATE TABLE, etc. statements are both hard-coded and hand-coded within the install program itself. When I asked them why they weren't using SQL Server's scripting capabilities to automatically generate the script, they said that SQL Server scripting is buggy and that it doesn't work properly. It was hard getting specific details but they said it would leave out indexes, for example. Now, I'm no SQL Server expert, but I've never had a problem using SQL Server's scripting capabilities. The database they use isn't that big - maybe 20-30 tables. So, before I make an issue out of it, I was wondering what everyone else's experience with SQL Server 2000's scripting features? If you've used SQL Servers scripting to recreate a database, does it work? Did you have issues?The script is the same whether you run it all together through one or more script files versus issuing the commands one be one from an installer. Perhaps they had trouble with the ordering of the index creating script so that it was being called at the wrong time. Either that or some other dependancy was out of synch. In any event it shouldn't be the script per se but the order that it is being applied.

Maybe if you can see one of the script that failed you could figure out what's going on.|||They're not using SQL Server 2000's scripting capabilities at all. All the DDL statements are hand coded in the VB.NET installation program. This seems like a time-consuming and error-prone process. They said that the reason why they are not using SQL Server's script generation feature is because it is broken.|||What I meant was that the script is just a bunch of DDL as is the hand-coding. If there is a problem with the Sql Server generated script it's probably that something is in the wrong order and/or they didn't include the necessary checkboxes to includes indices "Options" tab.

But if their hand-generated DDL is working fine then I wouldn't mess with it. What they might want to do is place it all into a text file (don't call it a script...) and have the installer read that in and execute it. This text file can then be an embedded resource inside the install so that it's not visible to users but can be accessed inside the installer as if it were just another file. This at least would get all the DDL together in one place and not need to be intermixed with the code. There is a walk through for this in the VS.Net help system in the Windows installer section.|||Right now, the client has just one developer. However, my consulting company will probably be adding another 3 developers. I'm the architect/lead developer; we don't have a DBA. Including myself, that means a total of 5 developers will probably be working on the project. I'm worried that with 5 developers, the database could get f**cked up. Obviously, I need some sort of process to ensure the database 'integrity' remains intact.|||Are you just working with a single instance of the database or do you have to worry about multiple instances of the same db installed at different locations?

Do you have SourceSafe or equivalent? You could keep the scripts or installer code that does it there in order to maintain some control and accountability. You'd perhaps want to make it fine grained enough so that it's not just one file that someone always has checked out. I like to have "If then else" scripts for most database objects. If a table doesn't exist, create it. If it does exist check each column. If a column doesn't exist create it. This is a bit of a pain to set up initially but is relatively easy to maintain thereafter.|||There will be multiple instances of the database at remote locations to worry about. These remote locations will be our customers.

No, they are not using source control. We already discussed the importance of source control with them. We'll set up VSS as soon as we get additional developers.

scripting

I am a developer working with an application that requires connections with
mulitple databases on their respective servers. My question is concerned
with updating the multiple databases to keep the schemas the same. I am
using scripts generate using the Create Script command in Enterprise Manager
to create a script to drop and create all the functions, views and procs and
that works without any problem. My question is how to deal with the tables.
Is there some method for automatically creating a script that will add
missing tables and modify exiiting ones based on the source tables
collection. I don't want to use drop and create as for the functions, etc,
because I will lose all the data in the tables. The database schemas must b
e
the same but the data in each is different. If I have to create the script
by hand, it is just as easy to modify the tables by hand so what I am lookin
g
for is a command that says, "make all the tables the same"
JBJB
http://www.karaszi.com/SQLServer/in...rate_script.asp
"JB" <JB@.discussions.microsoft.com> wrote in message
news:3126220C-6899-4EDB-92C5-F3CB34AC9017@.microsoft.com...
>I am a developer working with an application that requires connections with
> mulitple databases on their respective servers. My question is concerned
> with updating the multiple databases to keep the schemas the same. I am
> using scripts generate using the Create Script command in Enterprise
> Manager
> to create a script to drop and create all the functions, views and procs
> and
> that works without any problem. My question is how to deal with the
> tables.
> Is there some method for automatically creating a script that will add
> missing tables and modify exiiting ones based on the source tables
> collection. I don't want to use drop and create as for the functions,
> etc,
> because I will lose all the data in the tables. The database schemas must
> be
> the same but the data in each is different. If I have to create the
> script
> by hand, it is just as easy to modify the tables by hand so what I am
> looking
> for is a command that says, "make all the tables the same"
>
> --
> JB|||You may find some of the Comparison tools useful. Most of them will compare,
and then create scripts to rectify the differences.
Object Comparison:
AdeptSQL Diff
AlfaAlfa Software - SQL Server Comparison Tool
ApexSQL - SQL Diff
Best SoftTool - SQL DBCompare
e-Dule - DB SynchroComp
PrimeLogics - DataVision 2007
Quest - SchemaCompare
RAC4SQL's QALite (Free)
Red Gate - SQL Compare
SQL Effects Clarity
TASC - SQL Delta
Teratrax Database Compare
TulsaSoft - SQL Examiner
Voltex Data Systems - SQLDBcontrol
XpressApps - sqlXpress Diff
xSQL Software - xSQL Object
Free Tools
RAC4SQL's QALite (Free)
SQL Effects Clarity CE Edition
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"JB" <JB@.discussions.microsoft.com> wrote in message
news:3126220C-6899-4EDB-92C5-F3CB34AC9017@.microsoft.com...
>I am a developer working with an application that requires connections with
> mulitple databases on their respective servers. My question is concerned
> with updating the multiple databases to keep the schemas the same. I am
> using scripts generate using the Create Script command in Enterprise
> Manager
> to create a script to drop and create all the functions, views and procs
> and
> that works without any problem. My question is how to deal with the
> tables.
> Is there some method for automatically creating a script that will add
> missing tables and modify exiiting ones based on the source tables
> collection. I don't want to use drop and create as for the functions,
> etc,
> because I will lose all the data in the tables. The database schemas must
> be
> the same but the data in each is different. If I have to create the
> script
> by hand, it is just as easy to modify the tables by hand so what I am
> looking
> for is a command that says, "make all the tables the same"
>
> --
> JB

scripting

I am a developer working with an application that requires connections with
mulitple databases on their respective servers. My question is concerned
with updating the multiple databases to keep the schemas the same. I am
using scripts generate using the Create Script command in Enterprise Manager
to create a script to drop and create all the functions, views and procs and
that works without any problem. My question is how to deal with the tables.
Is there some method for automatically creating a script that will add
missing tables and modify exiiting ones based on the source tables
collection. I don't want to use drop and create as for the functions, etc,
because I will lose all the data in the tables. The database schemas must be
the same but the data in each is different. If I have to create the script
by hand, it is just as easy to modify the tables by hand so what I am looking
for is a command that says, "make all the tables the same"
JB
JB
http://www.karaszi.com/SQLServer/info_generate_script.asp
"JB" <JB@.discussions.microsoft.com> wrote in message
news:3126220C-6899-4EDB-92C5-F3CB34AC9017@.microsoft.com...
>I am a developer working with an application that requires connections with
> mulitple databases on their respective servers. My question is concerned
> with updating the multiple databases to keep the schemas the same. I am
> using scripts generate using the Create Script command in Enterprise
> Manager
> to create a script to drop and create all the functions, views and procs
> and
> that works without any problem. My question is how to deal with the
> tables.
> Is there some method for automatically creating a script that will add
> missing tables and modify exiiting ones based on the source tables
> collection. I don't want to use drop and create as for the functions,
> etc,
> because I will lose all the data in the tables. The database schemas must
> be
> the same but the data in each is different. If I have to create the
> script
> by hand, it is just as easy to modify the tables by hand so what I am
> looking
> for is a command that says, "make all the tables the same"
>
> --
> JB
|||You may find some of the Comparison tools useful. Most of them will compare,
and then create scripts to rectify the differences.
Object Comparison:
AdeptSQL Diff
AlfaAlfa Software - SQL Server Comparison Tool
ApexSQL - SQL Diff
Best SoftTool - SQL DBCompare
e-Dule - DB SynchroComp
PrimeLogics - DataVision 2007
Quest - SchemaCompare
RAC4SQL's QALite (Free)
Red Gate - SQL Compare
SQL Effects Clarity
TASC - SQL Delta
Teratrax Database Compare
TulsaSoft - SQL Examiner
Voltex Data Systems - SQLDBcontrol
XpressApps - sqlXpress Diff
xSQL Software - xSQL Object
Free Tools
RAC4SQL's QALite (Free)
SQL Effects Clarity CE Edition
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"JB" <JB@.discussions.microsoft.com> wrote in message
news:3126220C-6899-4EDB-92C5-F3CB34AC9017@.microsoft.com...
>I am a developer working with an application that requires connections with
> mulitple databases on their respective servers. My question is concerned
> with updating the multiple databases to keep the schemas the same. I am
> using scripts generate using the Create Script command in Enterprise
> Manager
> to create a script to drop and create all the functions, views and procs
> and
> that works without any problem. My question is how to deal with the
> tables.
> Is there some method for automatically creating a script that will add
> missing tables and modify exiiting ones based on the source tables
> collection. I don't want to use drop and create as for the functions,
> etc,
> because I will lose all the data in the tables. The database schemas must
> be
> the same but the data in each is different. If I have to create the
> script
> by hand, it is just as easy to modify the tables by hand so what I am
> looking
> for is a command that says, "make all the tables the same"
>
> --
> JB

scripting

I am a developer working with an application that requires connections with
mulitple databases on their respective servers. My question is concerned
with updating the multiple databases to keep the schemas the same. I am
using scripts generate using the Create Script command in Enterprise Manager
to create a script to drop and create all the functions, views and procs and
that works without any problem. My question is how to deal with the tables.
Is there some method for automatically creating a script that will add
missing tables and modify exiiting ones based on the source tables
collection. I don't want to use drop and create as for the functions, etc,
because I will lose all the data in the tables. The database schemas must be
the same but the data in each is different. If I have to create the script
by hand, it is just as easy to modify the tables by hand so what I am looking
for is a command that says, "make all the tables the same"
--
JBJB
http://www.karaszi.com/SQLServer/info_generate_script.asp
"JB" <JB@.discussions.microsoft.com> wrote in message
news:3126220C-6899-4EDB-92C5-F3CB34AC9017@.microsoft.com...
>I am a developer working with an application that requires connections with
> mulitple databases on their respective servers. My question is concerned
> with updating the multiple databases to keep the schemas the same. I am
> using scripts generate using the Create Script command in Enterprise
> Manager
> to create a script to drop and create all the functions, views and procs
> and
> that works without any problem. My question is how to deal with the
> tables.
> Is there some method for automatically creating a script that will add
> missing tables and modify exiiting ones based on the source tables
> collection. I don't want to use drop and create as for the functions,
> etc,
> because I will lose all the data in the tables. The database schemas must
> be
> the same but the data in each is different. If I have to create the
> script
> by hand, it is just as easy to modify the tables by hand so what I am
> looking
> for is a command that says, "make all the tables the same"
>
> --
> JB|||You may find some of the Comparison tools useful. Most of them will compare,
and then create scripts to rectify the differences.
Object Comparison:
AdeptSQL Diff
AlfaAlfa Software - SQL Server Comparison Tool
ApexSQL - SQL Diff
Best SoftTool - SQL DBCompare
e-Dule - DB SynchroComp
PrimeLogics - DataVision 2007
Quest - SchemaCompare
RAC4SQL's QALite (Free)
Red Gate - SQL Compare
SQL Effects Clarity
TASC - SQL Delta
Teratrax Database Compare
TulsaSoft - SQL Examiner
Voltex Data Systems - SQLDBcontrol
XpressApps - sqlXpress Diff
xSQL Software - xSQL Object
Free Tools
RAC4SQL's QALite (Free)
SQL Effects Clarity CE Edition
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"JB" <JB@.discussions.microsoft.com> wrote in message
news:3126220C-6899-4EDB-92C5-F3CB34AC9017@.microsoft.com...
>I am a developer working with an application that requires connections with
> mulitple databases on their respective servers. My question is concerned
> with updating the multiple databases to keep the schemas the same. I am
> using scripts generate using the Create Script command in Enterprise
> Manager
> to create a script to drop and create all the functions, views and procs
> and
> that works without any problem. My question is how to deal with the
> tables.
> Is there some method for automatically creating a script that will add
> missing tables and modify exiiting ones based on the source tables
> collection. I don't want to use drop and create as for the functions,
> etc,
> because I will lose all the data in the tables. The database schemas must
> be
> the same but the data in each is different. If I have to create the
> script
> by hand, it is just as easy to modify the tables by hand so what I am
> looking
> for is a command that says, "make all the tables the same"
>
> --
> JB

Wednesday, March 28, 2012

Script your data in a T-SQL format (which can be executed in Query Analyzer)

Hi all,

i'm working with SQL Server for about 4 years, and i have ever
searching for a tool which can script my DATA in a T-SQL format to
copy them very easy from one server to another.

For example scripts like that:
IF NOT EXISTS (...)
INSERT INTO (...)
ELSE
UPDATE (...)

I have never found a tool like that, so i write a it by myself.
This software is a free version in the moment, you can download it on
my website:

http://www.sqlscripter.com

..NET Framework on your client PC is required.
Have fun with it ...

ThomasThomas (postmaster@.sqlscripter.com) writes:
> i'm working with SQL Server for about 4 years, and i have ever
> searching for a tool which can script my DATA in a T-SQL format to
> copy them very easy from one server to another.
> For example scripts like that:
> IF NOT EXISTS (...)
> INSERT INTO (...)
> ELSE
> UPDATE (...)
>
> I have never found a tool like that, so i write a it by myself.
> This software is a free version in the moment, you can download it on
> my website:
> http://www.sqlscripter.com
> .NET Framework on your client PC is required.
> Have fun with it ...

I don't want to belittle your work, but the standard reference for this kind
of thing is SQL Server MVP Naryana Vyas Kondreddi's utility at
http://vyaskn.tripod.com/code.htm#inserts.

I don't think he supports UPDATE though.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Erland Sommarskog <sommar@.algonet.se> wrote in message news:<Xns93B2EBD6198ECYazorman@.127.0.0.1>...
> Thomas (postmaster@.sqlscripter.com) writes:
> > i'm working with SQL Server for about 4 years, and i have ever
> > searching for a tool which can script my DATA in a T-SQL format to
> > copy them very easy from one server to another.
> > For example scripts like that:
> > IF NOT EXISTS (...)
> > INSERT INTO (...)
> > ELSE
> > UPDATE (...)
> > I have never found a tool like that, so i write a it by myself.
> > This software is a free version in the moment, you can download it on
> > my website:
> > http://www.sqlscripter.com
> > .NET Framework on your client PC is required.
> > Have fun with it ...
> I don't want to belittle your work, but the standard reference for this kind
> of thing is SQL Server MVP Naryana Vyas Kondreddi's utility at
> http://vyaskn.tripod.com/code.htm#inserts.
> I don't think he supports UPDATE though.

Hi Erland,

thank you for your comment, the SP from "SQL Server MVP Naryana Vyas
Kondreddi" is very powerful ! Unfortunately only INSERTS are
supported, and in most of my cases, i need more functionality.

However, thanks for this link, good to know !

Thomas

Friday, March 23, 2012

Script to combine multiple rows into 1 single row

Hi,

I'm working on a system migration and I need to combine data from multiple
rows (with the same ID) into one comma separated string. This is how the
data is at the moment:

Company_ID Material
0x00C00000000053B86 Lead
0x00C00000000053B86 Sulphur
0x00C00000000053B86 Concrete

I need it in the following format:
Company_ID Material
0x00C00000000053B86 Lead, Sulphur, Concrete

There is no definite number of materials per Company.

I have read the part of
http://www.sommarskog.se/arrays-in-sql.html#iterative that talks about 'The
Iterative Method' but my knowledge of SQL is very limited and I don't know
how to use this code to get what I need.

Can anyone help me?Mintyman (mintyman@.ntlworld.com) writes:

Quote:

Originally Posted by

I'm working on a system migration and I need to combine data from multiple
rows (with the same ID) into one comma separated string. This is how the
data is at the moment:
>
Company_ID Material
0x00C00000000053B86 Lead
0x00C00000000053B86 Sulphur
0x00C00000000053B86 Concrete
>
I need it in the following format:
Company_ID Material
0x00C00000000053B86 Lead, Sulphur, Concrete
>
There is no definite number of materials per Company.
>
I have read the part of
http://www.sommarskog.se/arrays-in-sql.html#iterative that talks about
'The Iterative Method' but my knowledge of SQL is very limited and I
don't know how to use this code to get what I need.


And that article covers the opposite process - unpacking the list.

Composing the list is less funny, because it produces a result which
violates basic principles in a relational database: no repeating groups.
That is not to say that it's a stupid thing to ask for; it's not strange
to ask for this format in reporting. I get a little nervous when you
say that you are working with system migraton, because that means that
someone will have to handle the comma-separated list on the other side,
and is not funny at all. But I assume that you don't have control over
that.

Anyway, to give a good answer to the question, I would need to know a
few more things:
o Which version of SQL Server?
o What is a reasonable upper limit of the comma-separated string? You
could determine the current max value with this query:

SELECT MAX(listlen), AVG(listlen)
FROM (SELECT SUM(len(Material) + 2)
FROM tbl
GROUP BY Company_ID) as a

o What is the datatype of Material? That is, is varchar or nvarchar?

--
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|||Hi Erland,

I hope it's not to late to get help on this one!

Here are the answers you are looking for:

1) I'm using SQL 2000
2) 40
3) nvarchar

To clarify the field names, it is 'material_name' instead of 'material' and
is 'to_company' instead of 'company_id'

Thanks!

Mintyman

"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns98A19BE4CF73BYazorman@.127.0.0.1...

Quote:

Originally Posted by

Mintyman (mintyman@.ntlworld.com) writes:

Quote:

Originally Posted by

>I'm working on a system migration and I need to combine data from
>multiple
>rows (with the same ID) into one comma separated string. This is how the
>data is at the moment:
>>
>Company_ID Material
>0x00C00000000053B86 Lead
>0x00C00000000053B86 Sulphur
>0x00C00000000053B86 Concrete
>>
>I need it in the following format:
>Company_ID Material
>0x00C00000000053B86 Lead, Sulphur, Concrete
>>
>There is no definite number of materials per Company.
>>
>I have read the part of
>http://www.sommarskog.se/arrays-in-sql.html#iterative that talks about
>'The Iterative Method' but my knowledge of SQL is very limited and I
>don't know how to use this code to get what I need.


>
And that article covers the opposite process - unpacking the list.
>
Composing the list is less funny, because it produces a result which
violates basic principles in a relational database: no repeating groups.
That is not to say that it's a stupid thing to ask for; it's not strange
to ask for this format in reporting. I get a little nervous when you
say that you are working with system migraton, because that means that
someone will have to handle the comma-separated list on the other side,
and is not funny at all. But I assume that you don't have control over
that.
>
Anyway, to give a good answer to the question, I would need to know a
few more things:
o Which version of SQL Server?
o What is a reasonable upper limit of the comma-separated string? You
could determine the current max value with this query:
>
SELECT MAX(listlen), AVG(listlen)
FROM (SELECT SUM(len(Material) + 2)
FROM tbl
GROUP BY Company_ID) as a
>
o What is the datatype of Material? That is, is varchar or nvarchar?
>
--
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

|||Mintyman (mintyman@.ntlworld.com) writes:

Quote:

Originally Posted by

I hope it's not to late to get help on this one!
>
Here are the answers you are looking for:
>
1) I'm using SQL 2000
2) 40
3) nvarchar


I the longest list would be 40 characters, this means that there are not
that many materials per company. Since you said no limit, I was afraid
that there was a risk that you could exceed the limit of 4000 for an
nvarchar. In that case, you would have been in real dire straits. Unless
you had been on SQL 2005 where this would have been much simpler.

Here is an example of a query that runs in Northwind. First run:

select max(cnt) from
(select OrderID, cnt = COUNT(*)
from [Order Details]
group by OrderID) s

(but translated to your database). This gives the longest list in number
of elements. In case of Northwind the returned number is 25 which is a tad
many. With a maximum of 40 characters per list, a maximum of seven seems
reasonable. Using that number, here is a query for Northwind that
returns a comma-separated lists per order:

SELECT OrderID,
MAX(CASE OD.rowno WHEN 1 THEN P.ProductName END) +
coalesce(MAX(CASE OD.rowno WHEN 2 THEN ', ' + P.ProductName END), '') +
coalesce(MAX(CASE OD.rowno WHEN 3 THEN ', ' + P.ProductName END), '') +
coalesce(MAX(CASE OD.rowno WHEN 4 THEN ', ' + P.ProductName END), '') +
coalesce(MAX(CASE OD.rowno WHEN 5 THEN ', ' + P.ProductName END), '') +
coalesce(MAX(CASE OD.rowno WHEN 6 THEN ', ' + P.ProductName END), '') +
coalesce(MAX(CASE OD.rowno WHEN 7 THEN ', ' + P.ProductName END), '')
FROM (SELECT a.OrderID, a.ProductID,
rowno = (SELECT COUNT(*)
FROM [Order Details] b
WHERE b.OrderID = a.OrderID
AND b.ProductID <= a.ProductID)
FROM [Order Details] a) AS OD
JOIN Products P ON P.ProductID = OD.ProductID
GROUP BY OD.OrderID
ORDER BY OD.OrderID

If your maximum number is 8, you will need to add one more line.

Caveat: the performance of this is not fantastic. The big culprit is
the SELECT that computes the row number. If you have millions and millions
of rows in that table, you may bave to find a different way to compute
the row number. One way would to be bounce the data over a temp table
with an IDENTITY column. But before you go that route, try a query like
the one above.

If you need to compose many of these queries, I would suggest that you
look into the third-party tool RAC, http://www.rac4sql.net/ which can
help you to generate such queries.

--
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|||Hi Erland,

Thanks for the script. The difference between the Northwind database and
mine is that all the data I want to get access to is in one table (unlike
Northwind where it is spread over [order details[ and [products]. I tried
modifying the script but it doesn't work:

SELECT to_company,
MAX(CASE OD.rowno WHEN 1 THEN Material_Name END) +
coalesce(MAX(CASE OD.rowno WHEN 2 THEN ', ' + Material_Name END), '') +
coalesce(MAX(CASE OD.rowno WHEN 3 THEN ', ' + Material_Name END), '') +
coalesce(MAX(CASE OD.rowno WHEN 4 THEN ', ' + Material_Name END), '') +
coalesce(MAX(CASE OD.rowno WHEN 5 THEN ', ' + Material_Name END), '') +
coalesce(MAX(CASE OD.rowno WHEN 6 THEN ', ' + Material_Name END), '') +
coalesce(MAX(CASE OD.rowno WHEN 7 THEN ', ' + Material_Name END), '')
FROM Material__Bridge AS OD
GROUP BY OD.to_company
ORDER BY OD.to_company

It says there is an invalid column name 'rowno' - I guess this is right
because there is no column with that name in my database! However, when I
check in Northwind, there isn't one called that there either!

Any ideas?

"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns98AED9C3878CYazorman@.127.0.0.1...

Quote:

Originally Posted by

Mintyman (mintyman@.ntlworld.com) writes:

Quote:

Originally Posted by

>I hope it's not to late to get help on this one!
>>
>Here are the answers you are looking for:
>>
>1) I'm using SQL 2000
>2) 40
>3) nvarchar


>
I the longest list would be 40 characters, this means that there are not
that many materials per company. Since you said no limit, I was afraid
that there was a risk that you could exceed the limit of 4000 for an
nvarchar. In that case, you would have been in real dire straits. Unless
you had been on SQL 2005 where this would have been much simpler.
>
Here is an example of a query that runs in Northwind. First run:
>
select max(cnt) from
(select OrderID, cnt = COUNT(*)
from [Order Details]
group by OrderID) s
>
(but translated to your database). This gives the longest list in number
of elements. In case of Northwind the returned number is 25 which is a tad
many. With a maximum of 40 characters per list, a maximum of seven seems
reasonable. Using that number, here is a query for Northwind that
returns a comma-separated lists per order:
>
SELECT OrderID,
MAX(CASE OD.rowno WHEN 1 THEN P.ProductName END) +
coalesce(MAX(CASE OD.rowno WHEN 2 THEN ', ' + P.ProductName END), '')
+
coalesce(MAX(CASE OD.rowno WHEN 3 THEN ', ' + P.ProductName END), '')
+
coalesce(MAX(CASE OD.rowno WHEN 4 THEN ', ' + P.ProductName END), '')
+
coalesce(MAX(CASE OD.rowno WHEN 5 THEN ', ' + P.ProductName END), '')
+
coalesce(MAX(CASE OD.rowno WHEN 6 THEN ', ' + P.ProductName END), '')
+
coalesce(MAX(CASE OD.rowno WHEN 7 THEN ', ' + P.ProductName END), '')
FROM (SELECT a.OrderID, a.ProductID,
rowno = (SELECT COUNT(*)
FROM [Order Details] b
WHERE b.OrderID = a.OrderID
AND b.ProductID <= a.ProductID)
FROM [Order Details] a) AS OD
JOIN Products P ON P.ProductID = OD.ProductID
GROUP BY OD.OrderID
ORDER BY OD.OrderID
>
If your maximum number is 8, you will need to add one more line.
>
Caveat: the performance of this is not fantastic. The big culprit is
the SELECT that computes the row number. If you have millions and millions
of rows in that table, you may bave to find a different way to compute
the row number. One way would to be bounce the data over a temp table
with an IDENTITY column. But before you go that route, try a query like
the one above.
>
If you need to compose many of these queries, I would suggest that you
look into the third-party tool RAC, http://www.rac4sql.net/ which can
help you to generate such queries.
>
>
>
--
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

|||Mintyman (mintyman@.ntlworld.com) writes:

Quote:

Originally Posted by

Thanks for the script. The difference between the Northwind database and
mine is that all the data I want to get access to is in one table (unlike
Northwind where it is spread over [order details[ and [products].


I could have done the script with product ids instead of product names
but that seemed boring.

Quote:

Originally Posted by

It says there is an invalid column name 'rowno' - I guess this is right
because there is no column with that name in my database! However, when I
check in Northwind, there isn't one called that there either!


The column rowno is defined in the derived table. I suggest that you study
my query a little closer, and try to understand what it's actually doing.

It might be that you want to be spoon-fed a solution, but I have this funny
idea that I like to help people to help themselves. That is, when I post a
solution, I hope that people do not only use it, but also try to understand
how it works, so that the next time they run into a similar problem, they
now have something in their toolbox that they can apply.

--
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|||Hi Erland,

I totally agree with not being spoon fed! I'm sorry I came across as wanting
to be. I'll try and work out what your script is doing :o) Thanks for your
help!

Mintyman

"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns98AF7D09BCD42Yazorman@.127.0.0.1...

Quote:

Originally Posted by

Mintyman (mintyman@.ntlworld.com) writes:

Quote:

Originally Posted by

>Thanks for the script. The difference between the Northwind database and
>mine is that all the data I want to get access to is in one table (unlike
>Northwind where it is spread over [order details[ and [products].


>
I could have done the script with product ids instead of product names
but that seemed boring.
>

Quote:

Originally Posted by

>It says there is an invalid column name 'rowno' - I guess this is right
>because there is no column with that name in my database! However, when I
>check in Northwind, there isn't one called that there either!


>
The column rowno is defined in the derived table. I suggest that you study
my query a little closer, and try to understand what it's actually doing.
>
It might be that you want to be spoon-fed a solution, but I have this
funny
idea that I like to help people to help themselves. That is, when I post a
solution, I hope that people do not only use it, but also try to
understand
how it works, so that the next time they run into a similar problem, they
now have something in their toolbox that they can apply.
>
--
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

|||Mintyman (mintyman@.ntlworld.com) writes:

Quote:

Originally Posted by

I totally agree with not being spoon fed! I'm sorry I came across as
wanting to be. I'll try and work out what your script is doing :o)


There is one thing I should have pointed out. In my query there was this
part:

(SELECT a.OrderID, a.ProductID,
rowno = (SELECT COUNT(*)
FROM [Order Details] b
WHERE b.OrderID = a.OrderID
AND b.ProductID <= a.ProductID)
FROM [Order Details] a) AS OD

That is a *derived table*. A derived table is logically a temp table in
the query so to speak, but not materialised, and the actually computation
order can be different as long as the result is the same. Derived tables
is an enormously powerful tool to build complex queries with, and saves
you from using real temp tables.

--
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

Wednesday, March 21, 2012

Script Task working in Visual Studio but not when the package is run by a job?

I have a script that changes the name of a file after a data upload. The script works fine if I execute the package in Visual Studio but when I run the file package from a SQL server job it does not rename the file. The data does get uploaded it just does not run the final script.

Any help would be appreciated.

Steve

Have you enabled logging to get the error?

The problems when running under Agent are mostly related to authentication and permissions issues - as the job probably runs under different credentials compared to interactive execution. Your options are either configure job to run under your account, or fix the permissions to allow job account to perform the operation that is failing.

See this KB for troubleshooting steps:
http://support.microsoft.com/kb/918760

Tuesday, March 20, 2012

script problem with POWER(x,y)

hi,

i can't see why the following field is not working correctly, ie: it's always coming up with 0:

((POWER((1+((((((POWER((1+(dbo_table1.APR/100)),(1/12)))-1))*12))/12)), dbo_table1.FREQUENCY)-1)*(12/dbo_table1.FREQUENCY))*100 AS YIELD

breaking the code down to a smaller formula it does work.

it looks like i can't have more calculations/references inside a POWER(x,y) function.

any comments would be appreciated.

thank you

The problem is integer math. This formula can never return anything other than zero because (1/12) = 0. Guessing at datatypes:

declare @.apr decimal (4,2)
declare @.frequency decimal(3,0)

set @.apr = 6.5
set @.frequency = 24

select ((POWER((1+((((((POWER((1+(@.apr/100)),(1.0/12.0)))-1))*12))/12)), @.frequency)-1)*(12/@.frequency))*100

This now returns:

6.711700

Hope this helps.

|||

thank you very much.

it has solved the issue

regards

Saturday, February 25, 2012

screenshots showing solution

After setting up
SQL2005express server with
mixed authentification I got the problem
1. I couldnt find a working ConnectionString
2. Studio2005 didnt connect to SQL any more
In the meantime I found that at least the following OLEDB-ConnectionString is working, when Im logged in as admin and can use Windows Autentication:
Provider='sqloledb'
Data Source='.' - without '/SQLEXPRESS' !!
Initial Catalog='MyDatabaseName'
Integrated Security='SSPI'
For details of SQL setup and code to test possible connetion strings
please see my homepage.
Martin
text of my question from 02 Dec 2005
I want to feed data of a C++ 6.0 project into SQL server and hoped that StudioExpress would give me the correct connection string in detail.
But my StudioExpress doesnt conntect to a SQL server configured for SQL-Authentication.
I made screenshote of the details an put them in subfolders of my homepage
http://home.arcor.SQL2005.
Does anybody know what exact connection string has to be used and which changes in StudioExpress AdvancedConnectionProperties are necessary to establish a working connection?

The above link contains a collection of screenshots with details of the problem.
In the meantime I did a standard setup of the whole StudioExpress2005 C# paket and put screenshots on my homepage where you can see the difference between standard setup and single SQLExpress setup in SQL-Authentication mode.
Martin