SQL notes

  • SELECT DISTINCT * INTO ##temp3 from ##temp2 --Use this if I want to INSERT rows AND create the destination table.
  • WHERE ##temp1.Area <> ##temp2.Area -- The 'WHERE <>' stops a cartesian join.
  • SELECT * INTO ##temp3 FROM (SELECT * FROM ##temp1 UNION SELECT * FROM ##temp2) un
  • strSQL = "IF OBJECT_ID('tblTempNIS_Qtrly_SumByRUN_and_crc', 'U') IS NOT NULL DROP TABLE tblTempNIS_Qtrly_SumByRUN_and_crc" ' This is not really a temp table because no #; so must DROP. It's a helper table. Couldn't use a temp because mData's Clear lose the data for the next query's use of it. ' The ", 'U' " is optional; it just insures that the object is a user table and not, for example, a sproc or other type of object.
  • ' To get all information about the @@Error value that is returned in the output parameter 
    ' From Master database 
    ' SELECT * FROM sysmessages WHERE Error = 2627 ' type value in rdoQy(3) here
  • --NWDSQL.BmrkSource.PendingPublish is empty for 2003-2004. Thus do this to populate it with rows from the other table:
    INSERT INTO BmrkSource.dbo.PendingPublish
    SELECT NEArea,IdentifierCode,EndDate,NumberOfPeople,Week,ItemCodeID,Ratio --Note: Published has 1 extra column at the end so I must list these out.
    FROM NELausData.dbo.Published
    WHERE NELausData.dbo.Published.EndDate LIKE '2004%'
    OR NELausData.dbo.Published.EndDate LIKE '2003%'
  • The INNER JOIN returns all rows from both tables where there is a match. If there are rows in first table that do not have matches in second table, those rows will not be listed.
  • The LEFT JOIN returns all the rows from the first table, even if there are no matches in the second table. If there are rows in first table that do not have matches in second table, those rows also will be listed.
  • The RIGHT JOIN returns all the rows from the second table, even if there are no matches in the first table. If there had been any rows in second table that did not have matches in first table, those rows also would have been listed.
  • SQL Server is phasing out text, ntext, and image. There’s no way to know how long SQL Server will support the older data types. Upgrade legacy applications to varchar, nvarchar, and varbinary.


How to get a DB and send it....

You can backup the DB from the server and then restore it on your local machine with installed MS SQL Server 2000. Then you can change or delete any pruned info from the DB on your local machine. Then you can backup the modified DB again and send that backup file (*.bak file). You can send this file with Gmail. There is about 3Gb available space.

    How to backup...
  1. Open Enterprise Manager
  2. Select source DB
  3. Right click on the selected DB. Select All Tasks->Backup Database... from the popup menu.
  4. Select File and filegroup and add destination file name in the Backup dialog.
  5. Press OK
    How to restore...
  1. Open Enterprise Manager
  2. Select Databases node
  3. Right click on this node. Select All Tasks->Restore Database... from the popup menu.
  4. Type database name in the Restore as database textbox
  5. Select Restore From Device
  6. Press Select Device
  7. Add backup file name
  8. Press OK
  9. In the Options tab check file pathes where your database will be restored.
  10. Press OK
  11. Select File and filegroup and add destination file name in the Backup dialog.
  12. Press OK


Excel: compare two Worksheets

Excel Macro
Compares the 2 Worksheets and pops up a workbook with the differences:
Steps: >Tools > Macro >Macros >Run

Excel Function
Function for comparing data: http://office.microsoft.com/en-us/excel/HP100625641033.aspx?pid=CH100645341033
Here's the Excel formula: =Exact(ColumnRow,ColumnRow)
An example: =EXACT(G5,P5) [First put both worksheets on the same worksheet, side by side.]
If the result is True data matches; if False data does not match

Also, I was told there is a way to recieve as output each row that was different using a "Filter".


SQL compare two tables

Difference in data stored in both tables (compare the difference between two tables)? You can use SQL to see rows which don't have a match in one table or the other:
Select col_a,col_b from table1 
where not exists 
    (select * from table2 
     where table1.col_a = table2.col_a and 
           table1.col_b = table2.col_b)

Repeat for table2.

further notes: --http://windowsitpro.com/Articles/ArticleID/14368/14368.html?Ad=1

Here is a way of comparing 2 tables that I tried at work:

-- DROP my new temp table if exists:
DROP TABLE ##EricsTempUnionOfMonthlyAndBiweekly

-- UNION the monthly & biweekly tbls into a temp table:
SELECT * INTO ##EricsTempUnionOfMonthlyAndBiweekly 
FROM (SELECT * FROM NISTempLowLevelMonthly 
UNION SELECT * FROM NISTempLowLevelBiweekly) un

-- Verify it:
SELECT * FROM ##EricsTempUnionOfMonthlyAndBiweekly --18347rows, yes this was a UNION of monthly & biweekly tables.

-- LEFT OUTER JOIN which means gives me the differences:
SELECT DISTINCT * FROM ##EricsTempUnionOfMonthlyAndBiweekly LEFT OUTER JOIN NISTemp_Qtrly_Detail 
ON NISTemp_Qtrly_Detail.SocSecNumber = ##EricsTempUnionOfMonthlyAndBiweekly.SocSecNumber -- 72573 rows


Debug sproc [MSSQL stored procedures] in VS

( Source: http://aspalliance.com/1131_Understanding_Object_Pooling_in_Microsoft_NET )

In VS at the Sever Explorer pane I right-click on my data connection that I added and select Modify Connection. Then in the Modify Connection dialog I clicked Advanced... and then in the Advanced Properties dialog I changed Pooling from True to False. Now it works. Also, in the configuration file, the Enable SQL Server debugging must be checked. Also, I have to add cmd.CommandTimeout=1800 to the VB's sproc area else it will time-out while clicking thru the sproc in the debugger.

But, when not debugging, in order to get the app to run to completion without crashing with the error Error while executing 'a batch cmd' at line 0 , I have to uncheck Enable SQL Server debugging and set Pooling back to True. --Because not pooling the connections hurts performance.

(Summary: You do the above to VS so that you can step through,and see, the database while debugging the code of an app in VS.)


To export DTS package, do the following:

  1. Open MS SQL Server Enterprise Manager
  2. Go to Local Packages
  3. Open some DTS Package
  4. In the DTS Package window from menu select item Package->Save As...
  5. In the Save DTS Package dialog
    • in the Location field select Structured Storage
    • File
    • in the File Name field set destination path of saved file
  6. Press OK button


Server 2000: Query Analyzer Tips & Tricks

You probably know that SQL Server stores metadata about all of the objects in a database. The system tables contain a wealth of information about column names, data types, identity seeds, and so on. But did you know that you can get that information with a single keystroke via Query Analyzer? Highlight the object name in any SQL statement and press Alt+F1. Figure 1 shows the results for a SQL Server table. If you don't have anything highlighted, Alt+F1 will give you information about the database itself. For an equally neat trick, highlight a SQL keyword and press Shift+F1; you'll go straight to the Books Online page that describes that keyword.


Getting .dll into Registry

  1. Download the file (MSRDO20.zip) to your desktop
  2. Unzip the content of the file (a file called MSRDO20.DLL) to your desktop
  3. Right now you should have a new file called MSRDO20.DLL that resides on your desktop
  4. Copy this file to your Windows System Folder; this folder is usually found inside your 'C:\Windows' folder. It is called 'C:\Windows\System' or 'System32'...
  5. Go to 'Start Menu' and choose 'Run'
  6. A new window titled 'Run' will open; type the line 'regsvr32.exe msrdo20.dll' and hit 'Enter'.
  7. A message will show up stating that the registration was successful.


Visual Studio Short-cuts

Most of the time, you will Step Into or Step Over commands in your stored procedures. The commands below apply to a single T-SQL line:

  • Step Into (F11): Use to single step through your code. (Move the yellow arrow down one statement.)
  • Step Over (F10): Useful if you have lines of code that perhaps modify data or call other procedures that you don't care about while debugging. For example, you may want to skip code that performs auditing.
  • Step Out (SHIFT+F11): Execute the rest of the stored procedure without pause.
  • Run to Cursor (CTRL+F10): Position the cursor to a point in your code and then hit CTRL-F10 to execute all code up to that point.
  • Continue or Start Debug or Run (F5): Start Debug. Or resumes execution until completion or until the next breakpoint.
  • Toggle Bookmark (CTRL+K)
  • Help (F1)

When you provide a summary of the class using XML comments, your class displays documentation about itself in appropriate places within Visual Studio, such as in the List Members box. Open the List Members box by selecting Edit | Intellisense | List Members from the main menu bar or by clicking the Display an Object Member List icon on the Text Editor toolbar.

Command Window debugging: Getting Values

To see the value of your variable whilst in Debug mode, use the "Command Window" (View >Other Windows >Command Window). Enter this: ? myVariableName


SQL: Bulk import

Bulk import (save file.txt as csv): LOAD DATA INFILE 'file.txt' INTO TABLE 'tblFoo' (col1, col2, col3)

BULK LOAD IMPORT ('c:\file.txt') INTO TABLE 'tblFoo' (col1, col2, col3)


SQL Server 2000: make table backup with DTS

  • Right-click the table
    • choose Export Data
  • Choose a destination
    • Server: "nwdsql" and Database: NELausData
  • copy table(s)
  • UserXRef"_bkup" (Destination)

Done, then refresh explorer to view.

header adsense code