Showing posts with label select. Show all posts
Showing posts with label select. Show all posts

Monday, March 12, 2012

Integers in SQL Server

I'm having a problem with the way integers are stored internally and how they are returned with a SELECT statement. In the project I'm working on, I have to move data from an Excel file to a database so I'm using DTS and a group of stored procs to pull this off. What happens is:
    DTS pull data from Excel into temp tables Stored proc pulls from the first temp table into a supertype table IDENTITY column assigns primary keys Stored proc then loads primary keys into a cursor and, starting with the LAST record, moves the PK into the appropriate subtype table and assigns the correct foreign key enteries. Next stored proc pulls data from the second temp table into the supertype tabel Repeat process until all data is stored
I'm starting with the last record so I know that it will be from the most recent transfer from a temp table. Unfortunately the order of records that the cursor returns is not the order that they were inserted into the database. So when I go to transfer the records with PK numbers 258 and 257 (the last 2 inserted into the supertype) I end up moving the records with PK's 204 and 203 (the last 2 in the cursor). Is there any way to sort the records as they go into or come out of the cursor so the smallest number is first and the largets number last? Also, there are probably a thousand ways to do this more efficiently so suggestions there are also welcome. Thanks
I think I figured out a solution that doesn't use cursors and won't be affected by internal storage issues. However, comments and ideas are still welcome.|||Having staging table(s) is a very good thing when it comes to data import/export. If you can, try to eliminate dts from the process to save some cpu cycles. You'd want to look up linked server/openquery/openrowset/opendatasource in book online/google for some details.

Integer Parameter

I have a parameter field with a data type of int. I want the users to be able to select one or 'all' from the dropdown list. Is there a wildcard for a integer datatype?

Dropdown list example:

All Project Ids

1001

1002

1003

1004

In cases like this I will frequently use a NULL parameter to designate 'all'; can that work for you in this case? An example of this would be something like this:

declare @.example table (integerCol integer)
insert into @.example values (1001)
insert into @.example values (1002)
insert into @.example values (1003)
insert into @.example values (1004)

declare @.anIntegerParm integer
set @.anIntegerParm = null
set @.anIntegerParm = 1002

if @.anIntegerParm is null

select * from @.example

else

select * from @.example
where integerCol = @.anIntegerParm

-- When @.anIntegerParm is null:

-- integerCol
-- --
-- 1001
-- 1002
-- 1003
-- 1004

-- When @.anIntegerParm = 1002:

-- integerCol
-- --
-- 1002

select * from @.example
where @.anIntegerParm is null
or @.anIntegerParm is not null
and integerCol = @.anIntegerParm

-- integerCol
-- --
-- 1002