Showing posts with label update. Show all posts
Showing posts with label update. Show all posts

Tuesday, March 27, 2012

duplicate rows

Hi,

I have the following records in my sql table and want to place all the duplicates in one row. I tried to do this with an update query but had no success. I can do this in access but can't figure out how to do it in sql. Any help would be great. Thanks.

qrycurrentrecords LNAME MAJOR PAPATHANASIOU 135 DEPASSQUALLO 147 BILGER 215 KLER 267 MAKO 305 PERRY 379 MILLER 379 BILLS 379 WANDER 424 FLANAGAN 440 KAUFFMAN 440 KALLIS 492 SHARKY 670

mr4100 wrote:

... want to place all the duplicates in one row.

Can you explain a litlle more about what you mean?

|||

if you take a look at the picture, you can see there are 3 codes of 379. there are other columns attached to this table i just didn't show all of them. I want to loop through the table and place the 3 rows with the same code in a row by themselves. I'm sending an email from the table and don't want to send 3 seperate emails, just one email containing data about all 3 rows. My task sends an email for each row in the table. I hope I didn't confuse you.

thanks,

|||

It is still not clear what your desired output may look like. I suggest that if would be helpful if you posted the table DDL, some sample data in the form of INSERT statements, and what the desired output looks like. Also the version of SQL Server would be helpful in finding a solution.

I'm assuming you have explored the various ways to use GROUP BY...

|||

The confusing part is when you say: duplicates. There are not duplicates here, but rather it is just a typical situation with (hopefully) a parent table and key (for major) and then the child rows with different names. The best resource for how to do this is here:

http://databases.aspfaq.com/general/how-do-i-concatenate-strings-from-a-column-into-a-single-row.html

The site is sadly an eyesore these days, but the information is still good. Use the 2005 version for sure if you are using 2005, it is really excellent and works nice.

|||

what i want to do is take these 3 seperate rows that are in the table below with the same major and put them into 1 row by themselves whether it would be updating this table or a new table by themselves,

start with this:

name major desc

john 45 eng.

mary 45 eng.

corey 25 math

rose 45 eng.

sue 15 mus.

end with this:

name major desc

john,mary,rose 45 eng.

corey 25 math

sue 15 mus.

|||

Here it is.. if you use sql server 2005

Code Snippet

Create Table #data (

[name] Varchar(100) ,

[major] Varchar(100) ,

[desc] Varchar(100)

);

Insert Into #data Values('john','45','eng.');

Insert Into #data Values('mary','45','eng.');

Insert Into #data Values('corey','25','math');

Insert Into #data Values('rose','45','eng.');

Insert Into #data Values('sue','15','mus.');

Select Distinct

Substring((Select ',' + name [text()] from #data sub where sub.major=main.major and sub.[desc]=main.[desc] For Xml Path('')),2,8000)

,major

,[desc]

from

#data main

|||

this works perfectly! Could you explain why you use the #data sub and #data main and for xml Path(")),2,8000)?

I would just like to know what it is does to make this work, I would have never figured this out.

Thanks,

Monday, March 19, 2012

Dumb trigger question

Table with last UpdatedBy Column and LastUpdatedOn Column.
Want to create Insert and Update Triggers that would automatically write
Logged in User Name and System Date Time to each field.
I Think User is SUSER_SNAME function and System date Is GETDATE() but can't
figure out how to write these two to row from within a trigger in table.
Any help greatly appreciated.
BobYou can use the inserted psuedo-table to identify the updated rows. One
method:
CREATE TRIGGER TR__Update_MyTable
ON MyTable FOR UPDATE
AS
UPDATE MyTable
SET UpdatedBy = SUSER_SNAME(),
LastUpdatedOn = GETDATE()
WHERE EXISTS
(
SELECT *
FROM inserted
WHERE inserted.PK = MyTable.PK
)
Hope this helps.
Dan Guzman
SQL Server MVP
"Bob" <bdufour@.sgiims.com> wrote in message
news:uwISI5NzFHA.3188@.TK2MSFTNGP14.phx.gbl...
> Table with last UpdatedBy Column and LastUpdatedOn Column.
> Want to create Insert and Update Triggers that would automatically write
> Logged in User Name and System Date Time to each field.
> I Think User is SUSER_SNAME function and System date Is GETDATE() but
> can't figure out how to write these two to row from within a trigger in
> table.
> Any help greatly appreciated.
> Bob
>|||Use an INSTEAD OF trigger. The deleted pseudotable is a copy of any
existing rows that are about to be updated, the inserted pseudotable
contains the new values. The deleted pseudotable will be empty if the
operation was INSERT. The inserted pseudotable will be empty if the
operation was DELETE. Both pseudotables will have the same number of rows
if the operation was UPDATE. The reason you should use an INSTEAD OF
trigger is that it allows you to change values before it hits the database.
The only problem with them is that the cascade kludge won't coexist, which
in my opinion is a good thing.
In an INSTEAD OF UPDATE trigger, you simply issue an update statement:
UPDATE tableName
SET column1 = inserted.column1,
column2 = inserted.column2,
..,
UpdatedBy = SUSER_SNAME(),
LastUpdatedOn = GETDATE()
FROM inserted
WHERE primaryKeyColumn = inserted.primaryKeyColumn
The only time this is a problem is if you're using natural keys. If that's
the case, then you need to test for multiple rows
put code similar to this at the top of the trigger
--Don't put anything before this!!!
DECLARE @._ROWCOUNT INT SET @._ROWCOUNT = @.@.ROWCOUNT
IF @._ROWCOUNT = 0 RETURN
IF @._ROWCOUNT > 1 AND (UPDATE(primaryKeyColumn1) OR
UPDATE(primaryKeyColumn2))
BEGIN
ROLLBACK TRANSACTION
RAISERROR('ATTEMPT TO UPDATE Primary Key using set-based operation', 16, 0)
RETURN
END
"Bob" <bdufour@.sgiims.com> wrote in message
news:uwISI5NzFHA.3188@.TK2MSFTNGP14.phx.gbl...
> Table with last UpdatedBy Column and LastUpdatedOn Column.
> Want to create Insert and Update Triggers that would automatically write
> Logged in User Name and System Date Time to each field.
> I Think User is SUSER_SNAME function and System date Is GETDATE() but
> can't figure out how to write these two to row from within a trigger in
> table.
> Any help greatly appreciated.
> Bob
>|||Thank you both very much
Bob
"Bob" <bdufour@.sgiims.com> wrote in message
news:uwISI5NzFHA.3188@.TK2MSFTNGP14.phx.gbl...
> Table with last UpdatedBy Column and LastUpdatedOn Column.
> Want to create Insert and Update Triggers that would automatically write
> Logged in User Name and System Date Time to each field.
> I Think User is SUSER_SNAME function and System date Is GETDATE() but
> can't figure out how to write these two to row from within a trigger in
> table.
> Any help greatly appreciated.
> Bob
>

Wednesday, March 7, 2012

dtsx load id list from xls

How to write a dtsx that loads a list of ids from an xls and incorporates
this list into sql ?
For example,
update table1 set column1 = false where id in ( {the list of ids } )On Mar 12, 7:30 pm, "John Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
> How to write a dtsx that loads a list of ids from an xls and incorporates
> this list into sql ?
> For example,
> update table1 set column1 = false where id in ( {the list of ids } )
These directions will get an excel spreadsheet into a table that you
can then do: 'update table1 set column1 = false where id in (select id
from NewlyCreatedImportTable)':
1. Create a new Integration Services project.
2. Right click in the 'Connection Managers' tab and select 'New
Connection...' from the drop-down list.
3. Select the 'EXCEL' type and the 'Add' button.
4. Select the 'Browse...' button to browse for the Excel file and
select the 'Open' button.
5. If the first line of the Excel file contains the column names,
select the radio button to the left of: "First row has column names"
then select the 'Ok' button.
6. Right click in the 'Connection Managers' tab and select 'New OLE
DB Connection...' from the drop-down list.
7. Select the 'New...' button and select a Server name from the drop-
down list and select 'OK' and select the 'OK' button again.
8. Drag and drop a 'Data Flow Task' control from the Toolbox onto the
'Control Flow' tab/pane.
9. Select the 'Data Flow' tab/pane.
10. Drag and drop an 'Excel Source' control from the Toolbox onto the
'Data Flow' tab/pane.
11. Right click the 'Excel Source' control and select 'Edit...'
12. Below 'OLE DB connection manager:' select the connection manager
created in steps 2-5.
13. Select the sheet number from the drop-down list below: "Name of
the Excel sheet:" and then select 'OK.'
14. Drag and drop an 'OLE DB Destination' control from the Toolbox
onto the 'Data Flow' tab/pane.
15. Drag the green arrow from the 'Excel Source' control to the 'OLE
DB Destination' control.
16. Right click the 'OLE DB Destination' control and select the
'Edit...' button
17. Below 'OLE DB connection manager:', select the connection control
created in steps 6-7.
18. Select the 'New...' button and edit the sql query for the table
name and column names of the new table to be created (NOTE: SSIS is
very picky on the conversion types so
change datatypes very carefully, if at all).
19. Select the 'Mappings' option from the left-hand-side and map the
Input Columns and Destination columns as desired.
20. Select the 'OK' button and select 'F5' to execute the new package.
Hope this helps.
Regards,
Enrique Martinez
Sr. SQL Server Developer

Friday, February 17, 2012

DTS using #temp tables

I have a rather complex SP that creates a #temp table and then populates tha
t
table using several select/inserts/update statements. I need the results of
this #table to be exported to external text file nightly so that our
mainframe FTPs can grab it.
DTS apparently wont let me use #temp tables. I get invalid object errors.
Ive heard of global ##temp, but even changing the table all the references
to be ##table isn’t solving the problem.
Is there commands I can place in the SP to create the external file without
using DTS, or can I define the #temp table in a way that DTS can see it?
Help appreciated
--
JP
.NET Software DeveloperHi
This seems similar to http://tinyurl.com/82fqd
Temporary tables have a limited scope see the section in:
http://msdn.microsoft.com/library/d...r />
_4hk5.asp
Depending on what you are trying to do, you may not require the temporary
table or you could use a permanent table that you clear down before each run
.
Alternatively using a derived table or use of the CASE statement may be
possible options.
John
"JP" wrote:

> I have a rather complex SP that creates a #temp table and then populates t
hat
> table using several select/inserts/update statements. I need the results o
f
> this #table to be exported to external text file nightly so that our
> mainframe FTPs can grab it.
> DTS apparently wont let me use #temp tables. I get invalid object errors.
> Ive heard of global ##temp, but even changing the table all the reference
s
> to be ##table isn’t solving the problem.
> Is there commands I can place in the SP to create the external file withou
t
> using DTS, or can I define the #temp table in a way that DTS can see it?
> Help appreciated
> --
> JP
> .NET Software Developer
>

Wednesday, February 15, 2012

dts to update table

Hi,
I am writing a dts package to update a sql table using data in a csv
file. I know that dts updates all mapped columns regard less of the column's
value has changed or not. I need to perform a special action when dts
actually changes any column's value but not when the column's value remains
unchanged. Where should I write a query to perform this action?
Thanks in advance.Hi
More information regarding how you are doing this may be useful. The
assumption is that you are loading the data into a staging table or using a
linked server. You also don't say what sort of action this is?
It might be that you want a trigger an checking the original values against
the updated ones, or you could find out which rows would be affected by
comparing the two tables before the update using the UPDLOCK hint to make
sure the data does not change until the end of your transaction.
John
"helpful sql" <nospam@.stopspam.com> wrote in message
news:us$aIcJDGHA.2644@.TK2MSFTNGP09.phx.gbl...
> Hi,
> I am writing a dts package to update a sql table using data in a csv
> file. I know that dts updates all mapped columns regard less of the
> column's value has changed or not. I need to perform a special action when
> dts actually changes any column's value but not when the column's value
> remains unchanged. Where should I write a query to perform this action?
> Thanks in advance.
>

dts that makes updates

hello, its posible that a dts looks the data and compara if the rows
exists in target database make a update of the row and else make a
insert... thanks...
Since DTS has a completely open programming interface, if you can write the
code for it, DTS can do it.
Mike
Mentor
Solid Quality Learning
http://www.solidqualitylearning.com
"hongo32" <hongo32es@.yahoo.com> wrote in message
news:1136565790.166829.213060@.g14g2000cwa.googlegr oups.com...
> hello, its posible that a dts looks the data and compara if the rows
> exists in target database make a update of the row and else make a
> insert... thanks...
>
|||If you have a set of triggers writing to an audit table which DTS uses as a
basis to figure out what has changed, yes.
Otherwise I would look at transactional replication for something like this.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"hongo32" <hongo32es@.yahoo.com> wrote in message
news:1136565790.166829.213060@.g14g2000cwa.googlegr oups.com...
> hello, its posible that a dts looks the data and compara if the rows
> exists in target database make a update of the row and else make a
> insert... thanks...
>
|||Yes, DTS is highly programmable, especially if you script it out to VB. You
can do anything.
But I usually follow a practice of DTSing into a table reserved specifically
for the DTS data, matching the input format, and then I do the moving to
working tables from there. I feel this gives me some advantages:
1) DTS can be programmed, but it is a pain in the butt comparatively.
2) It gives me an exact record of what I received, so I am not wondering
whether an error was because of wrong/incomplete data or a wrong process.
3) It keeps the DTS as simple as possible so you have fewer failures.
4) The update process is clearly separated from the transfer process so it
is easier to debug.
"hongo32" <hongo32es@.yahoo.com> wrote in message
news:1136565790.166829.213060@.g14g2000cwa.googlegr oups.com...
> hello, its posible that a dts looks the data and compara if the rows
> exists in target database make a update of the row and else make a
> insert... thanks...
>
|||Thanks a lot, i am worried because the tables are very big and i don't
wan't translate all the content if i don't need it.
|||If it was me, and it was feasible to do so, I would probably prefer to spend
the money on disk space and do it the way I usually do. I have programmed
DTS a few times and never found it especially enjoyable. It always seemed to
cost more in terms of my time than the extra disk space would be.
But that's just me and my situations. Your mileage may vary.
How much data are we talking about here?
"hongo32" <hongo32es@.yahoo.com> wrote in message
news:1136574548.819640.129250@.g49g2000cwa.googlegr oups.com...
> Thanks a lot, i am worried because the tables are very big and i don't
> wan't translate all the content if i don't need it.
>