Skip to content

Database

Discussions on database access, SQL, and ADO

This category can be followed from the open social web via the handle database@forum.codeproject.com

17.1k Topics 61.8k Posts
  • Pivot?

    question database
    4
    0 Votes
    4 Posts
    0 Views
    A
    Tada... -- Copy Article stuff DECLARE @List VARCHAR(1000), @Sql VarChar(5000) SET @List = '' SET @Sql = '' IF EXISTS (SELECT * FROM tempdb.sys.objects WHERE name LIKE '#tblX%' AND type IN (N'U')) DROP TABLE [dbo].#tblX CREATE TABLE #tblX(MixDate DateTime, NoMixes Int, MixPlant_Name VarChar(50) ) INSERT [#tblX] SELECT DISTINCT(mp.MixDate), SUM(mp.NoMixes) As NoMixes, Mixplant.MixPlant_Name FROM MixProduction mp INNER JOIN MixPlant ON mp.MixPlant_ID = Mixplant.ID And MixPlant.IsActive =1 WHERE mp.MixDate BETWEEN '2/1/2011' AND '2/28/2011' Group By Mixplant.MixPlant_Name, mp.MixDate DECLARE @tblY TABLE(MixPlant_Name VarChar(50)) INSERT @tblY SELECT DISTINCT MixPlant_Name FROM #tblX ORDER BY MixPlant_Name SELECT @List = ISNULL(@List, '') + CASE WHEN ISNULL(@List,'') = '' THEN '[' + MixPlant_Name + ']' ELSE ',[' + MixPlant_Name + ']' END FROM @tblY ORDER BY MixPlant_Name SET @Sql = 'SELECT MixDate, ' + @List + CHAR(13) SET @Sql = @Sql + 'FROM (SELECT MixDate, NoMixes, MixPlant_Name ' + CHAR(13) SET @Sql = @Sql + 'FROM #tblX ) As P ' + CHAR(13) SET @Sql = @Sql + 'PIVOT (SUM(NoMixes) FOR MixPlant_Name IN (' + @List + ')) As Pvt' + CHAR(13) -- test output --print @sql --SELECT * FROM #tblX -- exucute dynamic sql Exec (@Sql) -- Drop Table DROP TABLE #tblX GO Thanks for prompting me to persist. Now to add a couple more columns to the pivot :-D I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife
  • Delta between records in one column

    help database tutorial question
    8
    0 Votes
    8 Posts
    0 Views
    U
    Okey it seems working and also in a very fast way. But I have a another question. In section With ordered as( ) I have some values declared... and now I dont know where can I put Declare and Set variable.. Ah Ok I didnt have ";".. before With or after declarations So now it is perfect;) modified on Monday, March 28, 2011 4:59 AM
  • 0 Votes
    3 Posts
    0 Views
    M
    Be very sure this is a real world issue, not just an excercise in theoretical design question on how to build a bulletproof data entry system. Over many years and some fairly high volume data entry apps I have never, not once, had to implement a locking structure to secure against simultaneous editing. I once wrote a system that kept track of the occurrence of data conflicts to assess their impact. The tracking system ran for 3 weeks and recorded 2 incidents, on a very high volume travel booking system, some twat of a manger had predicted multiple conflicts every hour. When the cost of the tracking was factored in management discarded it. So make sure you (probably some manager somewhere) are not just wasting your time on unrealistic problems. Never underestimate the power of human stupidity RAH
  • Multiple selection from the same table

    help database
    7
    0 Votes
    7 Posts
    0 Views
    W
    Luc Pattyn wrote: The enquirer will have to make up his mind and tell us what he really wants That's true :) The need to optimize rises from a bad design.My articles[^]
  • dynamically change column name

    database
    12
    0 Votes
    12 Posts
    0 Views
    J
    PIEBALDconsult wrote: That's generally a rather poor scheme, unless the reports are very specialized and something like Crystal ( X| ) won't do. Presumably you are comparing the difference between creating a report engine versus using an existing commercial/free one. In either case the engine itself contains the headers, while the data comes from the database. PIEBALDconsult wrote: I wrote such a reporting system on my last job and there is one at my new job. Neither has the ability to alter the column headings so it must all be done in the query. The engine simply outputs the contents of a DataTable. I can add the ability to mine a couple of different ways. I have written report engines and used several others including Crystal. Whether the headers need to be modified (presumably by the user) has nothing to do with where they originate. Nor does it have anything to do with what customizable features are presented to the user. What is has to do with is separation of concerns. Your argument could lead to the supposition that one should use quoted identifiers for most tables because most tables will be used in a report and so one might as well start with the report header name.
  • 0 Votes
    8 Posts
    0 Views
    K
    But wouldn't this be worth a vote ...
  • Need help with updating columns

    database help question announcement
    7
    0 Votes
    7 Posts
    0 Views
    K
    UPDATE table SET colB = DECODE( SIGN( colA ), -1, colA, 0 )
  • Using System.Data.SQLite from the new site

    sqlite com question workspace
    5
    0 Votes
    5 Posts
    0 Views
    P
    I have had to give up on the broken official version and I have my own version here: https://github.com/PaulS/SQLiteDotNetProvider It has most of the code changes from the official version, but will still build. (There are almost no changes.) Their version has missing files, and won't build an install. It's really about the same as 1.0.66, but compiles with sqlite 3.7.5. When the official version is working again I would probably go with that.
  • FInd Sundays only

    help question
    9
    0 Votes
    9 Posts
    0 Views
    W
    One possible reason could be if you have either statistics io or statistics time on in Management Studio. This would cause a flood in the messages tab since every insert is reported separately to Studio. This, of course, won't happen with CTE which is handled as a single statement thus reporting only one statistics. The need to optimize rises from a bad design.My articles[^]
  • Capture error return by MS Sql

    database question sql-server com sysadmin
    6
    0 Votes
    6 Posts
    0 Views
    W
    Hmm, what happens if you try to execute the statement dynamically. Put it into a string and then use exec to run the statement. Unfortunately I don't have SQL Server 2000 to test this issue... The need to optimize rises from a bad design.My articles[^]
  • How to spread traffic on a table

    database question sales help tutorial
    21
    0 Votes
    21 Posts
    0 Views
    D
    Database Snapshot? http://msdn.microsoft.com/en-us/library/ms175876.aspx[^] Also consider pre-generate the commonly used ad-hoc queries with heavy usage on the database so as not to tie down the actual database.
  • Sum of all rows

    database help
    5
    0 Votes
    5 Posts
    0 Views
    C
    Mika Wendelius wrote: Good idea, perhaps someday I'll contribute an article touching this subject. Yeah. If I learn the stuff correctly, I'll give a try too. :) I appreciate your help all the time... CodingLover :)
  • 0 Votes
    6 Posts
    0 Views
    W
    The first rule of transactions is: Never have a conversation with the user while a transaction is active! So what you should do is to ask all the necessary questions before you start the db operations, perhaps show what's in the database now and how it's going to be changed etc. After the last yes/no question, then do all the operations at once and if everything goes fine, commit otherwise rollback. In my opinion (I know many disagree) Read uncommitted should be banned for good. In a well designed system there's no need for RU isolation level and as you mentioned many of the database products don't even support it (inlcude Oracle in your list). Having UI conversations etc while you have placed locks in the database typically causes prolonged transactions, slows the system down and decreases parallelism because of locking issues. The need to optimize rises from a bad design.My articles[^]
  • 0 Votes
    4 Posts
    0 Views
    R
    It's possible your upgrade just installed the standard version alongside the express, with its own named instance. That's what the R2 Express edition did to my machine. Using the management tools, look for another instance on the host. If you find another, export the databases from the express version, then import them to the new one. Will Rogers never met me.
  • 0 Votes
    4 Posts
    0 Views
    D
    By using an right join it worked, the only problem was that it would have to change a lot of code because all the column indexes are different. I tried reversing the two section of the SQL Statement and came up with: SELECT TOP 1 * FROM [Order Status] WHERE OrderStatusID > " & CurrentRecordID & " ORDER BY OrderStatusID JOIN (SELECT * FROM Lead_Free_Certs) t ON t.[Project Num] = CAST([Order Status].[Project #] AS nvarchar(255)) However I'm getting an error about an error near the JOIN but I don't know what it is.
  • 0 Votes
    3 Posts
    0 Views
    W
    Here's one quite good check-list you should go through: http://blogs.msdn.com/b/sql_protocols/archive/2008/04/30/steps-to-troubleshoot-connectivity-issues.aspx[^] Few things to check also are: - is the server actually using port 1433 or something else - is the SQL Server a named instance. If it is, do you specify the name of the instance - and of course are you using the right credentials. The need to optimize rises from a bad design.My articles[^]
  • Cross-Tabulations with multiple tables

    database help
    6
    0 Votes
    6 Posts
    0 Views
    M
    This article[^] has helped a number of people, you need to do the thinking to apply the logic to your specific requirement. One of the beuties of CP is that there may be an article out there with, or close to your specific requirement which makes it dramatically easier to relate it to your problem. Never underestimate the power of human stupidity RAH
  • Connect Dababase from Windows7 to windows 2008.

    question database sysadmin
    6
    0 Votes
    6 Posts
    0 Views
    J
    Bernhard Hiller wrote: By the way, when trying telnet on my Win7 computer, I get a "command not found" message... You need to turn it on. Go to controlpanel, programs and features, turn windows features on or off. List of common misconceptions
  • Fail to access to my SQL file ( mdf )

    csharp database sql-server wcf linq
    2
    0 Votes
    2 Posts
    0 Views
    W
    Hi, Have a look at this conversation: http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/fbf32aa8-2125-4919-be81-3baa399e2e0d[^]. There are specific steps defined to get rid of the problem. If the root cause in your problem is the same this could help. The need to optimize rises from a bad design.My articles[^]
  • 0 Votes
    3 Posts
    0 Views
    W
    Hi, when connected to SQL Server, executing sp_helpdb[^] without any parameters lists all the databases available to the login. Best regards, mika The need to optimize rises from a bad design.My articles[^]