Showing posts with label fields. Show all posts
Showing posts with label fields. Show all posts

Thursday, March 29, 2012

copy on condition

i have two tables A and B with the same fields,

If the id field of table B equals id field in Table A i need to update th edata for that id row.
If the id field doesn;t match then i need to insert a new record in tale A for that id

that is i need to perform insertion or updation into table A depending on table B data
Can anyone give me some idea how to start?

You need two statements.
UPDATE A SET col1 = b.col1, col2 = b.col2, ... FROM A INNER JOIN B on A.pkcol = B.pkcol
INERT INTO A (pkcol, col1, col2, col3)
SELECT pkcol, col1, col2, col3
FROM B
WHERE pkcol NOT IN (SELECT pkcol FROM A)|||

Thank you very much Alex!
One more question, I have millions of data in the table, will the 2 queries work well for such huge data
.I mean performance wise. I have created indexes .

|||You'll just have to try it to find out. No way of guessing that without knowing what the tables are, how the indexes are, hardware, load, etc ...

Tuesday, March 27, 2012

copy fields of one table to the field structure of another table

How can I copy fields (not records) from one table to the field structure of
another table (in the same database on a sql server 2000)? So that I have
not to re-type the name and properties of the fields and to be sure the name
and properties of the fields are the same in both tables.
Thanks,
HugoScript Table definition and create new table with different name.
In Query Analizer right click on table -> Script Object to New Window As ->
Create. Type new name for table and run script.
"Educo Gent" wrote:

> How can I copy fields (not records) from one table to the field structure
of
> another table (in the same database on a sql server 2000)? So that I have
> not to re-type the name and properties of the fields and to be sure the na
me
> and properties of the fields are the same in both tables.
> Thanks,
> Hugo
>
>|||SELECT * FROM SourceTable
Into DestinationTable WHERE 1=0
Remeber that it will not copy the keys and indexes
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Educo Gent" <educo.gent@.skynet.be> wrote in message
news:ndHOd.8101$Ll6.561275@.phobos.telenet-ops.be...
> How can I copy fields (not records) from one table to the field structure
> of another table (in the same database on a sql server 2000)? So that I
> have not to re-type the name and properties of the fields and to be sure
> the name and properties of the fields are the same in both tables.
> Thanks,
> Hugo
>|||Hi Roji
The syntax is wrong
> SELECT * FROM SourceTable
> Into DestinationTable WHERE 1=0
SELECT * INTO Dest FROM Source WHERE 1=0
"Roji. P. Thomas" <thomasroji@.gmail.com> wrote in message
news:O0bvQS2DFHA.1292@.TK2MSFTNGP10.phx.gbl...
> SELECT * FROM SourceTable
> Into DestinationTable WHERE 1=0
> Remeber that it will not copy the keys and indexes
> --
> Roji. P. Thomas
> Net Asset Management
> https://www.netassetmanagement.com
>
> "Educo Gent" <educo.gent@.skynet.be> wrote in message
> news:ndHOd.8101$Ll6.561275@.phobos.telenet-ops.be...
structure
>|||You are right Uri. Thanks
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:%23QWD0W2DFHA.3504@.TK2MSFTNGP12.phx.gbl...
> Hi Roji
> The syntax is wrong
>
> SELECT * INTO Dest FROM Source WHERE 1=0
> "Roji. P. Thomas" <thomasroji@.gmail.com> wrote in message
> news:O0bvQS2DFHA.1292@.TK2MSFTNGP10.phx.gbl...
> structure
>

Tuesday, March 20, 2012

Copy Database with Microsoft.SqlServer.Management.Smo.Transfer breaks identity columns

Hello

I have a task to copy at runtime "etalon" database inside one same SQL 2005 server. Everythings ok except identity fields: identity breaks in new database.

I use such code:

Transfer xfr = new Transfer(db);
xfr.CopyAllObjects = true;
xfr.Options.ContinueScriptingOnError = true;
xfr.Options.NoIdentities = false;
xfr.Options.NoCollation = true;
xfr.Options.Default = true;
xfr.Options.Indexes = true;
xfr.Options.DriDefaults = true;
xfr.Options.DriAllKeys = true;
xfr.Options.DriForeignKeys = true;
xfr.Options.DriIndexes = true;
xfr.Options.DriPrimaryKey = true;
xfr.Options.DriUniqueKeys = true;
xfr.CopyAllDefaults = true;
xfr.DestinationDatabase = "db2";
xfr.DestinationServer = srv.Name;
xfr.DestinationLoginSecure = true;
xfr.CopySchema = true;
xfr.CopyAllUsers = true;
xfr.CopyData = true;

when I try to create just script by xfr.ScriptTransfer() I see correct sql with IDENTITY.

Thanks for help

Vladislav

This is a known issue with TransferData(), which is slated to be fixed in service pack 1. ScriptTransfer generates the correct T-SQL for identity columns. If you are only moving schema, then ScriptTransfer() should meet your needs.

Peter

|||Peter,

This explains why my backups aren't correct, but I still have another problem. I also need the database created with transfer to have the foreign keys copied.

What do you suggest?

Chris
|||

You can accomplish this by setting the following property:

xfr.Options.DriForeignKeys = true;

However, you might want to just specify:

xfr.Options.DriAllKeys = true;

Peter Saddow

|||Thanks!
|||

Hello

Has it really been fixed in SP1?

We have running Sql Server's on Version 9.0.2047.
I try to use Smo.Transfer to ship selected tables and data between the servers.

Beside NoIdentities I tried a lot of the option settings.
But I never get the identity property to 'Yes' on the target system.

The transfer.ScriptTransfer(); shows the correct script e.g. 'CREATE TABLE [dbo].[tableName] (colA Int IDENTITY(1,1) ...)
But logging the DataTransferEventArgs.Message shows that transfer.TransferData(); ignores the identity while creating the table.

Any advice?

|||i worked fine with me...when i installed the SP1 the TransferData() transfered tables with its identities

Copy Database with Microsoft.SqlServer.Management.Smo.Transfer breaks identity columns

Hello

I have a task to copy at runtime "etalon" database inside one same SQL 2005 server. Everythings ok except identity fields: identity breaks in new database.

I use such code:

Transfer xfr = new Transfer(db);
xfr.CopyAllObjects = true;
xfr.Options.ContinueScriptingOnError = true;
xfr.Options.NoIdentities = false;
xfr.Options.NoCollation = true;
xfr.Options.Default = true;
xfr.Options.Indexes = true;
xfr.Options.DriDefaults = true;
xfr.Options.DriAllKeys = true;
xfr.Options.DriForeignKeys = true;
xfr.Options.DriIndexes = true;
xfr.Options.DriPrimaryKey = true;
xfr.Options.DriUniqueKeys = true;
xfr.CopyAllDefaults = true;
xfr.DestinationDatabase = "db2";
xfr.DestinationServer = srv.Name;
xfr.DestinationLoginSecure = true;
xfr.CopySchema = true;
xfr.CopyAllUsers = true;
xfr.CopyData = true;

when I try to create just script by xfr.ScriptTransfer() I see correct sql with IDENTITY.

Thanks for help

Vladislav

This is a known issue with TransferData(), which is slated to be fixed in service pack 1. ScriptTransfer generates the correct T-SQL for identity columns. If you are only moving schema, then ScriptTransfer() should meet your needs.

Peter

|||Peter,

This explains why my backups aren't correct, but I still have another problem. I also need the database created with transfer to have the foreign keys copied.

What do you suggest?

Chris|||

You can accomplish this by setting the following property:

xfr.Options.DriForeignKeys = true;

However, you might want to just specify:

xfr.Options.DriAllKeys = true;

Peter Saddow

|||Thanks!|||

Hello

Has it really been fixed in SP1?

We have running Sql Server's on Version 9.0.2047.
I try to use Smo.Transfer to ship selected tables and data between the servers.

Beside NoIdentities I tried a lot of the option settings.
But I never get the identity property to 'Yes' on the target system.

The transfer.ScriptTransfer(); shows the correct script e.g. 'CREATE TABLE [dbo].[tableName] (colA Int IDENTITY(1,1) ...)
But logging the DataTransferEventArgs.Message shows that transfer.TransferData(); ignores the identity while creating the table.

Any advice?

|||i worked fine with me...when i installed the SP1 the TransferData() transfered tables with its identities

Copy Database with Microsoft.SqlServer.Management.Smo.Transfer breaks identity columns

Hello

I have a task to copy at runtime "etalon" database inside one same SQL 2005 server. Everythings ok except identity fields: identity breaks in new database.

I use such code:

Transfer xfr = new Transfer(db);
xfr.CopyAllObjects = true;
xfr.Options.ContinueScriptingOnError = true;
xfr.Options.NoIdentities = false;
xfr.Options.NoCollation = true;
xfr.Options.Default = true;
xfr.Options.Indexes = true;
xfr.Options.DriDefaults = true;
xfr.Options.DriAllKeys = true;
xfr.Options.DriForeignKeys = true;
xfr.Options.DriIndexes = true;
xfr.Options.DriPrimaryKey = true;
xfr.Options.DriUniqueKeys = true;
xfr.CopyAllDefaults = true;
xfr.DestinationDatabase = "db2";
xfr.DestinationServer = srv.Name;
xfr.DestinationLoginSecure = true;
xfr.CopySchema = true;
xfr.CopyAllUsers = true;
xfr.CopyData = true;

when I try to create just script by xfr.ScriptTransfer() I see correct sql with IDENTITY.

Thanks for help

Vladislav

This is a known issue with TransferData(), which is slated to be fixed in service pack 1. ScriptTransfer generates the correct T-SQL for identity columns. If you are only moving schema, then ScriptTransfer() should meet your needs.

Peter

|||Peter,

This explains why my backups aren't correct, but I still have another problem. I also need the database created with transfer to have the foreign keys copied.

What do you suggest?

Chris|||

You can accomplish this by setting the following property:

xfr.Options.DriForeignKeys = true;

However, you might want to just specify:

xfr.Options.DriAllKeys = true;

Peter Saddow

|||Thanks!|||

Hello

Has it really been fixed in SP1?

We have running Sql Server's on Version 9.0.2047.
I try to use Smo.Transfer to ship selected tables and data between the servers.

Beside NoIdentities I tried a lot of the option settings.
But I never get the identity property to 'Yes' on the target system.

The transfer.ScriptTransfer(); shows the correct script e.g. 'CREATE TABLE [dbo].[tableName] (colA Int IDENTITY(1,1) ...)
But logging the DataTransferEventArgs.Message shows that transfer.TransferData(); ignores the identity while creating the table.

Any advice?

|||i worked fine with me...when i installed the SP1 the TransferData() transfered tables with its identities

Copy Database with Microsoft.SqlServer.Management.Smo.Transfer breaks identity columns

I have a task to copy at runtime "etalon" database inside one same SQL 2005 server. Everythings ok except identity fields: identity breaks in new database.

I use such code:

Transfer xfr = new Transfer(db);
xfr.CopyAllObjects = true;
xfr.Options.WithDependencies = true;
xfr.Options.ContinueScriptingOnError = true;
xfr.Options.NoIdentities = false;
xfr.Options.NoCollation = true;
xfr.DestinationDatabase = "Clarina_N";
xfr.DestinationServer = srv.Name;
xfr.DestinationLoginSecure = true;
xfr.CopySchema = true;
xfr.CopyAllUsers = true;
xfr.TransferData();

when I try to create just script by xfr.ScriptTransfer() I see correct sql with IDENTITY.

Thanks for help

Vladislav

And same problem with defaults even when
xfr.Option.Default = true;

|||This has nothing to do with SSIS. You should try posting to the SMO forum.

Thanks,
Mattsqlsql

Copy Database with Microsoft.SqlServer.Management.Smo.Transfer breaks identity columns

Hello

I have a task to copy at runtime "etalon" database inside one same SQL 2005 server. Everythings ok except identity fields: identity breaks in new database.

I use such code:

Transfer xfr = new Transfer(db);
xfr.CopyAllObjects = true;
xfr.Options.ContinueScriptingOnError = true;
xfr.Options.NoIdentities = false;
xfr.Options.NoCollation = true;
xfr.Options.Default = true;
xfr.Options.Indexes = true;
xfr.Options.DriDefaults = true;
xfr.Options.DriAllKeys = true;
xfr.Options.DriForeignKeys = true;
xfr.Options.DriIndexes = true;
xfr.Options.DriPrimaryKey = true;
xfr.Options.DriUniqueKeys = true;
xfr.CopyAllDefaults = true;
xfr.DestinationDatabase = "db2";
xfr.DestinationServer = srv.Name;
xfr.DestinationLoginSecure = true;
xfr.CopySchema = true;
xfr.CopyAllUsers = true;
xfr.CopyData = true;

when I try to create just script by xfr.ScriptTransfer() I see correct sql with IDENTITY.

Thanks for help

Vladislav

This is a known issue with TransferData(), which is slated to be fixed in service pack 1. ScriptTransfer generates the correct T-SQL for identity columns. If you are only moving schema, then ScriptTransfer() should meet your needs.

Peter

|||Peter,

This explains why my backups aren't correct, but I still have another problem. I also need the database created with transfer to have the foreign keys copied.

What do you suggest?

Chris|||

You can accomplish this by setting the following property:

xfr.Options.DriForeignKeys = true;

However, you might want to just specify:

xfr.Options.DriAllKeys = true;

Peter Saddow

|||Thanks!|||

Hello

Has it really been fixed in SP1?

We have running Sql Server's on Version 9.0.2047.
I try to use Smo.Transfer to ship selected tables and data between the servers.

Beside NoIdentities I tried a lot of the option settings.
But I never get the identity property to 'Yes' on the target system.

The transfer.ScriptTransfer(); shows the correct script e.g. 'CREATE TABLE [dbo].[tableName] (colA Int IDENTITY(1,1) ...)
But logging the DataTransferEventArgs.Message shows that transfer.TransferData(); ignores the identity while creating the table.

Any advice?

|||i worked fine with me...when i installed the SP1 the TransferData() transfered tables with its identities

Thursday, March 8, 2012

copy data from 1 table to other in stored procedure in sql server

Hi there,

Can u please tell me how to copy data from table A(database A) to table B(databaseB) which table A contain 10 fields but table B consist of 11 fields. I have to insert current date and time into another field in Table B (which has extra field compare to tableA) automatically every hour or so.
Please help.
ThanxOriginally posted by michaelfg81
Hi there,

Can u please tell me how to copy data from table A(database A) to table B(databaseB) which table A contain 10 fields but table B consist of 11 fields. I have to insert current date and time into another field in Table B (which has extra field compare to tableA) automatically every hour or so.
Please help.
Thanx

if my code in sql query analyszer like below

insert into esipquery.dbo.tsip_wip
select assy_lot_no, location_id, traveler_type, ase_lot_no, device_type, process_id,
process_rev, sequence, in_qty, out_qty, status, check_in_date, check_in_time,
check_out_date, check_out_time, package_grp, cust_id, ex_lot, getdate() as create_time
from esipdata.dbo.vSIP_LOT_TRACK_WIP

and i get error sound like this:

Server: Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'PK_tSIP_WIP'. Cannot insert duplicate key in object 'tSIP_WIP'.
The statement has been terminated.

after i made a check on it, i found out that one of the primary key in table esipquery.dbo.tsip_wip
is not a primary key in table esipdata.dbo.vSIP_LOT_TRACK_WIP

How can i fix it?
Can anyone pls help me
Thanx|||Post the table structure, including the primary keys and indexes. You have a primary key defined on the table you are inserting into. You keep trying to insert a value into that table, which is alread there. This won't work.

Saturday, February 25, 2012

Copy a row with one change?

Hello. I have a table that has 3 fields as a primary key - JobID (int),
PONumber (char 16) and PORevision (tinyint). I need to make a new row with
the same data except the PORevision field should be increased by 1. I have
tried a few things but nothing seems to work. I could do it in my vb.net
code but I am sure it would be better in the sql database.
Thanks,
Gerry
INSERT INTO YourTable (jobid, ponumber, porevision, other_col, ...)
SELECT jobid, ponumber, porevision+1, other_col, ...
FROM YourTable
WHERE ...
David Portas
SQL Server MVP

Copy a row with one change?

Hello. I have a table that has 3 fields as a primary key - JobID (int),
PONumber (char 16) and PORevision (tinyint). I need to make a new row with
the same data except the PORevision field should be increased by 1. I have
tried a few things but nothing seems to work. I could do it in my vb.net
code but I am sure it would be better in the sql database.
Thanks,
GerryINSERT INTO YourTable (jobid, ponumber, porevision, other_col, ...)
SELECT jobid, ponumber, porevision+1, other_col, ...
FROM YourTable
WHERE ...
David Portas
SQL Server MVP
--

Copy a row with one change?

Hello. I have a table that has 3 fields as a primary key - JobID (int),
PONumber (char 16) and PORevision (tinyint). I need to make a new row with
the same data except the PORevision field should be increased by 1. I have
tried a few things but nothing seems to work. I could do it in my vb.net
code but I am sure it would be better in the sql database.
Thanks,
GerryINSERT INTO YourTable (jobid, ponumber, porevision, other_col, ...)
SELECT jobid, ponumber, porevision+1, other_col, ...
FROM YourTable
WHERE ...
--
David Portas
SQL Server MVP
--|||Or you could make the PORevision field an identity and
the system would do it for you.
>--Original Message--
>Hello. I have a table that has 3 fields as a primary
key - JobID (int),
>PONumber (char 16) and PORevision (tinyint). I need to
make a new row with
>the same data except the PORevision field should be
increased by 1. I have
>tried a few things but nothing seems to work. I could
do it in my vb.net
>code but I am sure it would be better in the sql
database.
>Thanks,
>Gerry
>
>.
>

Sunday, February 19, 2012

Converting to SQL Server

So where I work is thinking about one day moving to SQL server. Right now they have indexed files that aren't normalized with repeating fields in them and lots of repeat data and blank space (so a customer number in one file may be stored literally in 10 other files that are easily realted). In the intrest of saving time and money I think that they will not normalize, index, or anything to any of these files. From what I hear it will be a straight field by field creation for the most part and preserving the primary keys.

My question: I keep thinking this is going to be massive hit on performance and maintaince. How much would converting in such a manner hurt the performance of their database and how much could it potentially add to maintaince?Well, I'd suggest this approach (field-by-field and "file-by-file") to be taken regardless of what the final plan is. Simply because it'll be much easier to work with once it's done. But as step 2 of course some effort of normalization needs to be applied. I bet all their apps are doing DML right from the front-end, right?

Converting to crosstab table

Hello there
I have table with 2 fields: table1(Field1, Field2)
I need to present it as crosstab in this way:
Original:
1, 1
2, 2
2, 3
3, 4
3, 5
3, 6
3, 7
Should look like this:
1, 1
2, 2, 3
3, 4, 5, 6
4, 7
How can i do this dinamicly?See if this helps.
http://groups.google.com/group/micr...br />
6dd9e73e
AMB
"Roy Goldhammer" wrote:

> Hello there
> I have table with 2 fields: table1(Field1, Field2)
> I need to present it as crosstab in this way:
> Original:
> 1, 1
> 2, 2
> 2, 3
> 3, 4
> 3, 5
> 3, 6
> 3, 7
> Should look like this:
> 1, 1
> 2, 2, 3
> 3, 4, 5, 6
> 4, 7
> How can i do this dinamicly?
>
>|||Can you explain the logic behind the result?
--
"Roy Goldhammer" wrote:

> Hello there
> I have table with 2 fields: table1(Field1, Field2)
> I need to present it as crosstab in this way:
> Original:
> 1, 1
> 2, 2
> 2, 3
> 3, 4
> 3, 5
> 3, 6
> 3, 7
> Should look like this:
> 1, 1
> 2, 2, 3
> 3, 4, 5, 6
> 4, 7
> How can i do this dinamicly?
>
>|||I think he had a typo in his sample data.
Roy, this is why producing something we can repro will cause less problems.
Please generate CREATE TABLE and INSERT statements instead of typing out
tabular data by hand, it will be much less prone to errors. See
http://www.aspfaq.com/5006
"Omnibuzz" <Omnibuzz@.discussions.microsoft.com> wrote in message
news:A4DFEBEC-BABA-4929-9821-3077466EFA2F@.microsoft.com...
> Can you explain the logic behind the result?
> --
>
>
> "Roy Goldhammer" wrote:
>|||We don't need your ddl statements to confirm that whatever
type of dynamic xtab you want you can do easily with Rac:)
www.rac4sql.net

Tuesday, February 14, 2012

Converting text to numeric

I have a database table with 2 numeric fields. The values to be inserted into these fields come from text boxes in an ASP.NET page. So in the parameters to add into these fields I simply use 'textbox1.text' & textbox2.text'.

One of the fields accepts the data within the text box and adds it into the appropriate numeric data field in the table. However on the other text box I get an error message 'Error converting data type nvarchar to numeric.'

Does anyone know why this occurs just on the one entry and not the other?, and if anyone has any suggestions on how to get around this please let me know.

Many ThanksWhat are the values of textbox1 and textbox2? If you have "fred" in textbox2, of course it cannot be converted to a numeric value.

Converting Sql7 to 2000 version

Can anyone tell where i can find any guide to prevent big error!! My problems
are date and string. When using datetime fields, before i used [yyyy-mm-dd]
format, now its no longer available!!! How cai i do?!!?! Must i covert all
stored procedure or code? Wich problems can i have with the new string limit?
Thanx all!!
Marko
Hi
Have you looked at CAST and CONVERT. Formatting a string as you want is is
still there.
How are you trying to use the data?
Regards
Mike
"Marko" wrote:

> Can anyone tell where i can find any guide to prevent big error!! My problems
> are date and string. When using datetime fields, before i used [yyyy-mm-dd]
> format, now its no longer available!!! How cai i do?!!?! Must i covert all
> stored procedure or code? Wich problems can i have with the new string limit?
> Thanx all!!
> Marko
|||Thanx, but all my Stored Procedure i use CONVERT to obtain 120th format,
yyyy-mm-dd.
With 2000 version this is not good, and i must correct all the procedure
with yyyymmdd: is it correct?
This is a problem for me because i must to correct many procedures and then
i must tu correct all the procedure call from ado Connection; for example
cn.execute ("MyProc 'yyyy-mm-dd'") must become cn.execute ("MyProc
'yyyymmdd'")
Can you help me?
Again , Thanx !!
Mark
"Mike Epprecht (SQL MVP)" wrote:
[vbcol=seagreen]
> Hi
> Have you looked at CAST and CONVERT. Formatting a string as you want is is
> still there.
> How are you trying to use the data?
> Regards
> Mike
> "Marko" wrote:

Converting Sql7 to 2000 version

Can anyone tell where i can find any guide to prevent big error!! My problem
s
are date and string. When using datetime fields, before i used [yyyy-mm-
dd]
format, now its no longer available!!! How cai i do'!!?! Must i covert all
stored procedure or code? Wich problems can i have with the new string limit
?
Thanx all!!
MarkoHi
Have you looked at CAST and CONVERT. Formatting a string as you want is is
still there.
How are you trying to use the data?
Regards
Mike
"Marko" wrote:

> Can anyone tell where i can find any guide to prevent big error!! My probl
ems
> are date and string. When using datetime fields, before i used [yyyy-m
m-dd]
> format, now its no longer available!!! How cai i do'!!?! Must i covert a
ll
> stored procedure or code? Wich problems can i have with the new string lim
it?
> Thanx all!!
> Marko|||Thanx, but all my Stored Procedure i use CONVERT to obtain 120th format,
yyyy-mm-dd.
With 2000 version this is not good, and i must correct all the procedure
with yyyymmdd: is it correct'
This is a problem for me because i must to correct many procedures and then
i must tu correct all the procedure call from ado Connection; for example
cn.execute ("MyProc 'yyyy-mm-dd'") must become cn.execute ("MyProc
'yyyymmdd'")
Can you help me?
Again , Thanx !!
Mark
"Mike Epprecht (SQL MVP)" wrote:
[vbcol=seagreen]
> Hi
> Have you looked at CAST and CONVERT. Formatting a string as you want is is
> still there.
> How are you trying to use the data?
> Regards
> Mike
> "Marko" wrote:
>

Converting Sql7 to 2000 version

Can anyone tell where i can find any guide to prevent big error!! My problems
are date and string. When using datetime fields, before i used [yyyy-mm-dd]
format, now its no longer available!!! How cai i do'!!?! Must i covert all
stored procedure or code? Wich problems can i have with the new string limit?
Thanx all!!
MarkoHi
Have you looked at CAST and CONVERT. Formatting a string as you want is is
still there.
How are you trying to use the data?
Regards
Mike
"Marko" wrote:
> Can anyone tell where i can find any guide to prevent big error!! My problems
> are date and string. When using datetime fields, before i used [yyyy-mm-dd]
> format, now its no longer available!!! How cai i do'!!?! Must i covert all
> stored procedure or code? Wich problems can i have with the new string limit?
> Thanx all!!
> Marko|||Thanx, but all my Stored Procedure i use CONVERT to obtain 120th format,
yyyy-mm-dd.
With 2000 version this is not good, and i must correct all the procedure
with yyyymmdd: is it correct'
This is a problem for me because i must to correct many procedures and then
i must tu correct all the procedure call from ado Connection; for example
cn.execute ("MyProc 'yyyy-mm-dd'") must become cn.execute ("MyProc
'yyyymmdd'")
Can you help me?
Again , Thanx !!
Mark
"Mike Epprecht (SQL MVP)" wrote:
> Hi
> Have you looked at CAST and CONVERT. Formatting a string as you want is is
> still there.
> How are you trying to use the data?
> Regards
> Mike
> "Marko" wrote:
> > Can anyone tell where i can find any guide to prevent big error!! My problems
> > are date and string. When using datetime fields, before i used [yyyy-mm-dd]
> > format, now its no longer available!!! How cai i do'!!?! Must i covert all
> > stored procedure or code? Wich problems can i have with the new string limit?
> > Thanx all!!
> > Marko

Sunday, February 12, 2012

Converting records into fields

Hello!
Is there any way in SQL to convert records in fields. Something like
creating a a record with 3 fields from a table with 3 records?
Thanks in advance,
Hugo MadureiraYes and no. What you've asked for is presumably one of two things:
1. Formatting for a report or display. Do that client side. SQL returns
tabular results to the client or middle tier but SQL doesn't control
how those results are formatted. Display of rows, columns, delimitting,
etc is all controlled by your client application. ADO for example
includes the GetString method to do exactly what you've asked.
2. Formatting for export to some external system. Do that in DTS, BCP
or another ETL tool. It is just a question of what delimiter you choose
for columns and rows.
Although yours is quite a common FAQ doing this in SQL has no obvious
advantage and quite a few divantages. We'd need more info to help
you with a specific bit of code for your case: What is the key of your
table? How do you want to define which row transposes to which column
in the result?
Meantime, here's an example:
CREATE TABLE foo (col INTEGER NOT NULL PRIMARY KEY, x INTEGER NOT NULL)
;
INSERT INTO foo (col,x) VALUES (1,777) ;
INSERT INTO foo (col,x) VALUES (2,888) ;
INSERT INTO foo (col,x) VALUES (3,999) ;
SELECT
MIN(CASE WHEN col = 1 THEN x END) AS col1,
MIN(CASE WHEN col = 2 THEN x END) AS col2,
MIN(CASE WHEN col = 3 THEN x END) AS col3
FROM foo ;
David Portas
SQL Server MVP
--|||Hi Hugo
This is usually best to do on the Front End, but if you can distinguish and
link the three records then you can use a self join to do this
e.g.
CREATE TABLE MyAttributes ( id int, type char(1), colour char(10) )
INSERT INTO MyAttributes ( id , type, colour )
SELECT 1, 'A', 'Pink'
UNION ALL SELECT 1, 'B', 'Red'
UNION ALL SELECT 1, 'C', 'Crimson'
UNION ALL SELECT 2, 'A', 'Cyan'
UNION ALL SELECT 2, 'B', 'Blue'
UNION ALL SELECT 2, 'C', 'Navy'
SELECT a.id, a.colour, b.colour, c.colour
FROM MyAttributes a
JOIN MyAttributes b ON a.id= b.id and b.type = 'B'
JOIN MyAttributes c ON a.id= c.id and c.type = 'C'
WHERE a.type = 'A'
John
"Hugo Madureira" wrote:

> Hello!
> Is there any way in SQL to convert records in fields. Something like
> creating a a record with 3 fields from a table with 3 records?
> Thanks in advance,
> Hugo Madureira
>|||Hugo
read this
http://www.aspfaq.com/show.asp?id=2462
--
Regards
R.D
--Knowledge gets doubled when shared
"Hugo Madureira" wrote:

> Hello!
> Is there any way in SQL to convert records in fields. Something like
> creating a a record with 3 fields from a table with 3 records?
> Thanks in advance,
> Hugo Madureira
>|||I'm working on a warehouse management system. My table represents the
products stored in a pallet. I don't know how many products are stored
in the pallet.
I want to copy the name of the products into a new table where the
fields are V01, V02, V02, ...
I've seen examples using case statement but I don't know beforehand the
values in the products name.
David Portas wrote:
> Yes and no. What you've asked for is presumably one of two things:
> 1. Formatting for a report or display. Do that client side. SQL returns
> tabular results to the client or middle tier but SQL doesn't control
> how those results are formatted. Display of rows, columns, delimitting,
> etc is all controlled by your client application. ADO for example
> includes the GetString method to do exactly what you've asked.
> 2. Formatting for export to some external system. Do that in DTS, BCP
> or another ETL tool. It is just a question of what delimiter you choose
> for columns and rows.
> Although yours is quite a common FAQ doing this in SQL has no obvious
> advantage and quite a few divantages. We'd need more info to help
> you with a specific bit of code for your case: What is the key of your
> table? How do you want to define which row transposes to which column
> in the result?
> Meantime, here's an example:
> CREATE TABLE foo (col INTEGER NOT NULL PRIMARY KEY, x INTEGER NOT NULL)
> ;
> INSERT INTO foo (col,x) VALUES (1,777) ;
> INSERT INTO foo (col,x) VALUES (2,888) ;
> INSERT INTO foo (col,x) VALUES (3,999) ;
> SELECT
> MIN(CASE WHEN col = 1 THEN x END) AS col1,
> MIN(CASE WHEN col = 2 THEN x END) AS col2,
> MIN(CASE WHEN col = 3 THEN x END) AS col3
> FROM foo ;
>|||Hugo
There is another article on web. Google for that. With this you can do that
--
Regards
R.D
--Knowledge gets doubled when shared
"Hugo Madureira" wrote:

> I'm working on a warehouse management system. My table represents the
> products stored in a pallet. I don't know how many products are stored
> in the pallet.
> I want to copy the name of the products into a new table where the
> fields are V01, V02, V02, ...
> I've seen examples using case statement but I don't know beforehand the
> values in the products name.
>
> David Portas wrote:
>|||> I want to copy the name of the products into a new table where the
> fields are V01, V02, V02, ...
Why? That sounds like a big design error. Lookup "repeating group" and
First Normal Form if you don't understand why. Product is a single
attribute and should be modelled in a single column.
David Portas
SQL Server MVP
--|||I know exactly what first normal form is. The point here is that my
table with all the products wouldn't be used to store information, just
as a list of products in the pallet to be printed.
David Portas wrote:
>
> Why? That sounds like a big design error. Lookup "repeating group" and
> First Normal Form if you don't understand why. Product is a single
> attribute and should be modelled in a single column.
>|||That was my first assumption too. So I refer you to my original answer:
Reporting isn't a database function.
Of course it's perfectly possible to implement these these things in
the database, but not usually desirable. Try the following or see the
link that R.D. posted.
SELECT
MIN(CASE WHEN col = 1 THEN productname END) AS col1,
MIN(CASE WHEN col = 2 THEN productname END) AS col2,
MIN(CASE WHEN col = 3 THEN productname END) AS col3
/* ... etc */
FROM
(SELECT P1.productname, COUNT(*) AS col
FROM northwind.dbo.products AS P1
JOIN northwind.dbo.products AS P2
ON P1.productname >= P2.productname
GROUP BY P1.productname) AS T ;
David Portas
SQL Server MVP
--

Friday, February 10, 2012

Converting Non-Unicode to Unicode

What is the best way to convert a large 24/7 database to Unicode? We have
various varchar and text fields that need to become nvarchar and ntext
respectively. I know I can alter the tables to do the varchars, but that
would have to be offline (looks like like about 2 hours per column on my
50 million record table). Text fields can't be converted with an alter so
they have to be copied to a new column. I was just wondering if anybody
has experience converting these.
Why not design another table with new datatypes and use DTS or BULK insert to import data from the old table.
Once data is propogated delete the old table and rename new to old to handle the application.
HTH
--
Satya SKJ
Visit http://www.sql-server-performance.com for tips and articles on Performance topic.
"Brad" wrote:

> What is the best way to convert a large 24/7 database to Unicode? We have
> various varchar and text fields that need to become nvarchar and ntext
> respectively. I know I can alter the tables to do the varchars, but that
> would have to be offline (looks like like about 2 hours per column on my
> 50 million record table). Text fields can't be converted with an alter so
> they have to be copied to a new column. I was just wondering if anybody
> has experience converting these.
>
|||In article <38E5F8AA-A720-4E27-A01F-9BB364263191@.microsoft.com>,
satyaskj@.yahoo.co.uk said...
> Why not design another table with new datatypes and use DTS or BULK insert to import data from the old table.
> Once data is propogated delete the old table and rename new to old to handle the application.
Because I need to do it as on-line as possible. If I do a copy I will
have to shut down the site before I begin and I know it will take quite a
while.

Converting Non-Unicode to Unicode

What is the best way to convert a large 24/7 database to Unicode? We have
various varchar and text fields that need to become nvarchar and ntext
respectively. I know I can alter the tables to do the varchars, but that
would have to be offline (looks like like about 2 hours per column on my
50 million record table). Text fields can't be converted with an alter so
they have to be copied to a new column. I was just wondering if anybody
has experience converting these.Why not design another table with new datatypes and use DTS or BULK insert t
o import data from the old table.
Once data is propogated delete the old table and rename new to old to handle
the application.
HTH
--
--
Satya SKJ
Visit http://www.sql-server-performance.com for tips and articles on Perform
ance topic.
"Brad" wrote:

> What is the best way to convert a large 24/7 database to Unicode? We have
> various varchar and text fields that need to become nvarchar and ntext
> respectively. I know I can alter the tables to do the varchars, but that
> would have to be offline (looks like like about 2 hours per column on my
> 50 million record table). Text fields can't be converted with an alter so
> they have to be copied to a new column. I was just wondering if anybody
> has experience converting these.
>|||In article <38E5F8AA-A720-4E27-A01F-9BB364263191@.microsoft.com>,
satyaskj@.yahoo.co.uk said...
> Why not design another table with new datatypes and use DTS or BULK insert
to import data from the old table.
> Once data is propogated delete the old table and rename new to old to handle the a
pplication.
Because I need to do it as on-line as possible. If I do a copy I will
have to shut down the site before I begin and I know it will take quite a
while.