Sunday, March 25, 2012
Change field size
I have a table with a number of nvarchar fields. One field in the midde was
1000 in length. I ran a query with:
ALTER TableName
ALTER ColumnName nvarchar(2000).
This apeared to increase the field size to 2000 (as seen in Server
Management Studio), but it will still not store any more than 1000 characters.
Have I done it incorrectly.
There appears to be still space in my record as I can add a field at the end
2000 long and that works correctly.
Thanks,
Steve.
Why do you say it only stores 1000 characters? Is it because what you are
seeing when you select it? If so then change the max size of the columns
characters returned in the options menu to support more. What does
MAX(DATALEN(YourColumn)) say for that column?
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"Sawlmgsj" <Sawlmgsj@.discussions.microsoft.com> wrote in message
news:1F7CC5B5-47B9-41B4-BD89-14263656BC0E@.microsoft.com...
> SQL 2005
> I have a table with a number of nvarchar fields. One field in the midde
> was
> 1000 in length. I ran a query with:
> ALTER TableName
> ALTER ColumnName nvarchar(2000).
> This apeared to increase the field size to 2000 (as seen in Server
> Management Studio), but it will still not store any more than 1000
> characters.
> Have I done it incorrectly.
> There appears to be still space in my record as I can add a field at the
> end
> 2000 long and that works correctly.
> Thanks,
> Steve.
>
|||Andrew - many thanks for your reply. That got me thinking and I checked the
(Delplhi) component I am using to link. It had field definitions in it and I
had forgotten to refresh it. Did that and now all is working correctly.
Thanks,
Steve.
"Andrew J. Kelly" wrote:
> Why do you say it only stores 1000 characters? Is it because what you are
> seeing when you select it? If so then change the max size of the columns
> characters returned in the options menu to support more. What does
> MAX(DATALEN(YourColumn)) say for that column?
> --
> Andrew J. Kelly SQL MVP
> Solid Quality Mentors
>
> "Sawlmgsj" <Sawlmgsj@.discussions.microsoft.com> wrote in message
> news:1F7CC5B5-47B9-41B4-BD89-14263656BC0E@.microsoft.com...
>
Change field size
I have a table with a number of nvarchar fields. One field in the midde was
1000 in length. I ran a query with:
ALTER TableName
ALTER ColumnName nvarchar(2000).
This apeared to increase the field size to 2000 (as seen in Server
Management Studio), but it will still not store any more than 1000 characters.
Have I done it incorrectly.
There appears to be still space in my record as I can add a field at the end
2000 long and that works correctly.
Thanks,
Steve.Why do you say it only stores 1000 characters? Is it because what you are
seeing when you select it? If so then change the max size of the columns
characters returned in the options menu to support more. What does
MAX(DATALEN(YourColumn)) say for that column?
--
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"Sawlmgsj" <Sawlmgsj@.discussions.microsoft.com> wrote in message
news:1F7CC5B5-47B9-41B4-BD89-14263656BC0E@.microsoft.com...
> SQL 2005
> I have a table with a number of nvarchar fields. One field in the midde
> was
> 1000 in length. I ran a query with:
> ALTER TableName
> ALTER ColumnName nvarchar(2000).
> This apeared to increase the field size to 2000 (as seen in Server
> Management Studio), but it will still not store any more than 1000
> characters.
> Have I done it incorrectly.
> There appears to be still space in my record as I can add a field at the
> end
> 2000 long and that works correctly.
> Thanks,
> Steve.
>|||Andrew - many thanks for your reply. That got me thinking and I checked the
(Delplhi) component I am using to link. It had field definitions in it and I
had forgotten to refresh it. Did that and now all is working correctly.
Thanks,
Steve.
"Andrew J. Kelly" wrote:
> Why do you say it only stores 1000 characters? Is it because what you are
> seeing when you select it? If so then change the max size of the columns
> characters returned in the options menu to support more. What does
> MAX(DATALEN(YourColumn)) say for that column?
> --
> Andrew J. Kelly SQL MVP
> Solid Quality Mentors
>
> "Sawlmgsj" <Sawlmgsj@.discussions.microsoft.com> wrote in message
> news:1F7CC5B5-47B9-41B4-BD89-14263656BC0E@.microsoft.com...
> > SQL 2005
> >
> > I have a table with a number of nvarchar fields. One field in the midde
> > was
> > 1000 in length. I ran a query with:
> >
> > ALTER TableName
> > ALTER ColumnName nvarchar(2000).
> >
> > This apeared to increase the field size to 2000 (as seen in Server
> > Management Studio), but it will still not store any more than 1000
> > characters.
> >
> > Have I done it incorrectly.
> >
> > There appears to be still space in my record as I can add a field at the
> > end
> > 2000 long and that works correctly.
> >
> > Thanks,
> > Steve.
> >
>
Change field size
I have a table with a number of nvarchar fields. One field in the midde was
1000 in length. I ran a query with:
ALTER TableName
ALTER ColumnName nvarchar(2000).
This apeared to increase the field size to 2000 (as seen in Server
Management Studio), but it will still not store any more than 1000 character
s.
Have I done it incorrectly.
There appears to be still space in my record as I can add a field at the end
2000 long and that works correctly.
Thanks,
Steve.Why do you say it only stores 1000 characters? Is it because what you are
seeing when you select it? If so then change the max size of the columns
characters returned in the options menu to support more. What does
MAX(DATALEN(YourColumn)) say for that column?
Andrew J. Kelly SQL MVP
Solid Quality Mentors
"Sawlmgsj" <Sawlmgsj@.discussions.microsoft.com> wrote in message
news:1F7CC5B5-47B9-41B4-BD89-14263656BC0E@.microsoft.com...
> SQL 2005
> I have a table with a number of nvarchar fields. One field in the midde
> was
> 1000 in length. I ran a query with:
> ALTER TableName
> ALTER ColumnName nvarchar(2000).
> This apeared to increase the field size to 2000 (as seen in Server
> Management Studio), but it will still not store any more than 1000
> characters.
> Have I done it incorrectly.
> There appears to be still space in my record as I can add a field at the
> end
> 2000 long and that works correctly.
> Thanks,
> Steve.
>|||Andrew - many thanks for your reply. That got me thinking and I checked the
(Delplhi) component I am using to link. It had field definitions in it and
I
had forgotten to refresh it. Did that and now all is working correctly.
Thanks,
Steve.
"Andrew J. Kelly" wrote:
> Why do you say it only stores 1000 characters? Is it because what you are
> seeing when you select it? If so then change the max size of the columns
> characters returned in the options menu to support more. What does
> MAX(DATALEN(YourColumn)) say for that column?
> --
> Andrew J. Kelly SQL MVP
> Solid Quality Mentors
>
> "Sawlmgsj" <Sawlmgsj@.discussions.microsoft.com> wrote in message
> news:1F7CC5B5-47B9-41B4-BD89-14263656BC0E@.microsoft.com...
>
Thursday, March 8, 2012
change a value in a field
I would like a quick and easy way to change a field value
If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
tableE where fieldD = '3122007' "
I get a ton of results
What I want to do is if fieldD = 3122007, I want to change that value to
<NULL>
How do I do that?
You can use UPDATE statement like this:
UPDATE tableE
SET fieldD = NULL
WHERE fieldD = 3122007
HTH,
Plamen Ratchev
http://www.SQLStudio.com
|||you want it to happen during the query or have the database itself
change the stored value when there's an insert with a specific number?
On Mar 14, 11:31 pm, "Johnfli" <j...@.ivhs.us> wrote:
> I have a table with several fields (like who doesn't)
> I would like a quick and easy way to change a field value
> If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
> tableE where fieldD = '3122007' "
> I get a ton of results
> What I want to do is if fieldD = 3122007, I want to change that value to
> <NULL>
> How do I do that?
|||"Johnfli" <john@.ivhs.us> wrote in message
news:uZEjPCpZHHA.1580@.TK2MSFTNGP05.phx.gbl...
>I have a table with several fields (like who doesn't)
> I would like a quick and easy way to change a field value
> If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
> tableE where fieldD = '3122007' "
> I get a ton of results
> What I want to do is if fieldD = 3122007, I want to change that value to
> <NULL>
>
> How do I do that?
>
If you mean change the field in the table, what Plamen posted is the way.
If you mean in the returned result set, you may want to look at CASE.
Greg Moore
SQL Server DBA Consulting
Email: sql (at) greenms.com http://www.greenms.com
|||worked perfectly!!
thank you
"Plamen Ratchev" <Plamen@.SQLStudio.com> wrote in message
news:O%23pVOsqZHHA.1400@.TK2MSFTNGP06.phx.gbl...
> You can use UPDATE statement like this:
> UPDATE tableE
> SET fieldD = NULL
> WHERE fieldD = 3122007
> HTH,
> Plamen Ratchev
> http://www.SQLStudio.com
>
change a value in a field
I would like a quick and easy way to change a field value
If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
tableE where fieldD = '3122007' "
I get a ton of results
What I want to do is if fieldD = 3122007, I want to change that value to
<NULL>
How do I do that?You can use UPDATE statement like this:
UPDATE tableE
SET fieldD = NULL
WHERE fieldD = 3122007
HTH,
Plamen Ratchev
http://www.SQLStudio.com|||you want it to happen during the query or have the database itself
change the stored value when there's an insert with a specific number?
On Mar 14, 11:31 pm, "Johnfli" <j...@.ivhs.us> wrote:
> I have a table with several fields (like who doesn't)
> I would like a quick and easy way to change a field value
> If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
> tableE where fieldD = '3122007' "
> I get a ton of results
> What I want to do is if fieldD = 3122007, I want to change that value to
> <NULL>
> How do I do that?|||"Johnfli" <john@.ivhs.us> wrote in message
news:uZEjPCpZHHA.1580@.TK2MSFTNGP05.phx.gbl...
>I have a table with several fields (like who doesn't)
> I would like a quick and easy way to change a field value
> If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
> tableE where fieldD = '3122007' "
> I get a ton of results
> What I want to do is if fieldD = 3122007, I want to change that value to
> <NULL>
>
> How do I do that?
>
If you mean change the field in the table, what Plamen posted is the way.
If you mean in the returned result set, you may want to look at CASE.
--
Greg Moore
SQL Server DBA Consulting
Email: sql (at) greenms.com http://www.greenms.com|||worked perfectly!!
thank you
"Plamen Ratchev" <Plamen@.SQLStudio.com> wrote in message
news:O%23pVOsqZHHA.1400@.TK2MSFTNGP06.phx.gbl...
> You can use UPDATE statement like this:
> UPDATE tableE
> SET fieldD = NULL
> WHERE fieldD = 3122007
> HTH,
> Plamen Ratchev
> http://www.SQLStudio.com
>
change a value in a field
I would like a quick and easy way to change a field value
If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
tableE where fieldD = '3122007' "
I get a ton of results
What I want to do is if fieldD = 3122007, I want to change that value to
<NULL>
How do I do that?You can use UPDATE statement like this:
UPDATE tableE
SET fieldD = NULL
WHERE fieldD = 3122007
HTH,
Plamen Ratchev
http://www.SQLStudio.com|||you want it to happen during the query or have the database itself
change the stored value when there's an insert with a specific number?
On Mar 14, 11:31 pm, "Johnfli" <j...@.ivhs.us> wrote:
> I have a table with several fields (like who doesn't)
> I would like a quick and easy way to change a field value
> If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
> tableE where fieldD = '3122007' "
> I get a ton of results
> What I want to do is if fieldD = 3122007, I want to change that value to
> <NULL>
> How do I do that?|||"Johnfli" <john@.ivhs.us> wrote in message
news:uZEjPCpZHHA.1580@.TK2MSFTNGP05.phx.gbl...
>I have a table with several fields (like who doesn't)
> I would like a quick and easy way to change a field value
> If I run a query that says " select fieldA, fieldB, fieldC, fieldD from
> tableE where fieldD = '3122007' "
> I get a ton of results
> What I want to do is if fieldD = 3122007, I want to change that value to
> <NULL>
>
> How do I do that?
>
If you mean change the field in the table, what Plamen posted is the way.
If you mean in the returned result set, you may want to look at CASE.
Greg Moore
SQL Server DBA Consulting
Email: sql (at) greenms.com http://www.greenms.com|||worked perfectly!!
thank you
"Plamen Ratchev" <Plamen@.SQLStudio.com> wrote in message
news:O%23pVOsqZHHA.1400@.TK2MSFTNGP06.phx.gbl...
> You can use UPDATE statement like this:
> UPDATE tableE
> SET fieldD = NULL
> WHERE fieldD = 3122007
> HTH,
> Plamen Ratchev
> http://www.SQLStudio.com
>
Saturday, February 25, 2012
Certificate Restore Not working.
I have been trying to use Encryption to encrypt a few key fields. So my first goal was to create all the items I would need to effectivly use encryption, to encrypt a few key fields.
First I created a sample sql script to create a certificate, create a table and insert some test data encrypted. And then made sure I could decrypt the data encrypted with the certificate.
Second step was to test the backing up of the certificate, droping the certificate to similate a restore of the database. And then a restore of the certificate, and then see if the it was still possible to decrypt the existing encrypted data with the restored certificate.
I receive no errors when restoreing the certificate, but it does not properly decrypt the exisiting data.
Can anyone help and point out my mistake?
--Start Sql--
--Create Sample Cert CREATE CERTIFICATE SampleCert1 ENCRYPTION BY PASSWORD = '728AC41753642403251BF8E7233EC0C' WITH SUBJECT = 'Sample Cert for Demo', EXPIRY_DATE = '04/18/2017'; --create Sample table with encrypted version CREATE TABLE Table_1 (Id int NOT NULL IDENTITY (1, 1), SSN_Encrypted varbinary(300) NOT NULL) ON [PRIMARY] GO --insert row with encrypted version. Insert into Table_1 (SSN_Encrypted) values (EncryptByCert(Cert_ID('SampleCert1'),'000-00-0000')) --returns '000-00-0000' for 'SampleCert1' since it was able to decrypt select SSN_Encrypted, cast(DecryptByCert(Cert_ID('SampleCert1'),SSN_Encrypted,N'728AC41753642403251BF8E7233EC0C') as varchar(12)) as 'SampleCert1' from Table_1 GO --backup Certificate BACKUP CERTIFICATE SampleCert1 TO FILE = 'D:\Backups\SampleCert.cer' WITH PRIVATE KEY ( FILE = 'D:\Backups\SampleCert.pvk' , DECRYPTION BY PASSWORD = N'728AC41753642403251BF8E7233EC0C' , ENCRYPTION BY PASSWORD = N'997jkhUbhk$w4ez0876hKHJH5gh' ); GO DROP CERTIFICATE SampleCert1; GO --Restore from backup. to simulate a restore from tape CREATE CERTIFICATE SampleCert1 FROM FILE = 'D:\Backups\SampleCert.cer' WITH PRIVATE KEY (FILE = 'D:\Backups\SampleCert.pvk', DECRYPTION BY PASSWORD = N'997jkhUbhk$w4ez0876hKHJH5gh'); --This should return the same ssn as was encrypted, if returns null the restore failed. select SSN_Encrypted, cast(DecryptByCert(Cert_ID('SampleCert1'),SSN_Encrypted,N'728AC41753642403251BF8E7233EC0C') as varchar(12)) as 'SampleCert1' from Table_1 --cleanup Removes Certificate from the system DROP CERTIFICATE SampleCert1; --removes the temporary table. Drop table Table_1I think I found out why your statement is not working as you expected.
In the following query, the certificate is re-created from the backup, but only a decryption password (used to open the PVK file) is specified, but no encryption password is present. This will create the CERTIFICATE in SQL Server with the private key protected by the DB master key.
CREATE CERTIFICATE SampleCert1
FROM FILE = 'D:\Backups\SampleCert.cer'
WITH PRIVATE KEY (FILE = 'D:\Backups\SampleCert.pvk',
DECRYPTION BY PASSWORD = N'997jkhUbhk$w4ez0876hKHJH5gh');
Instead of
CREATE CERTIFICATE SampleCert1
FROM FILE = 'D:\Backups\SampleCert.cer'
WITH PRIVATE KEY (FILE = 'D:\Backups\SampleCert.pvk',
DECRYPTION BY PASSWORD = N'997jkhUbhk$w4ez0876hKHJH5gh',
ENCRYPTION BY PASSWORD = '728AC41753642403251BF8E7233EC0C');
You can verify if my assumption is correct by running the following statement:
SELECT name, pvt_key_encryption_type_desc FROM sys.certificates
If I am right, you can just change the protection mechanism for the existing CERTIFICATE:
ALTER CERTIFICATE SampleCert1
WITH PRIVATE KEY ( ENCRYPTION BY PASSWORD = '728AC41753642403251BF8E7233EC0C' );
Let us know if this information helped.
Thanks,
-Raul Garcia
SDE/T
SQL Server Engine
|||Well today is not my lucky day I am afraid. I can't verify yet.
Last night we rolled out a policy to enforce passwords complexity, and length requirements.
So I am geting an error just trying to run the same script, even though I am using NT Authentication, and have updated my password to conform to the new policy.
Msg 15118, Level 16, State 1, Line 1
Password validation failed. The password does not meet Windows policy requirements because it is not complex enough.
Does Create Certificate use a different security creditial then the loged in Users?
I even tried using SA with the following set. with the same results.
Code Snippet
Alter Login [sa] with CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
Any more idea's where I need to look to figure this out?
|||Disabling the password policy checks is not available for cryptographic objects (certificates, keys, etc.), the only part of the policy enforced in such objects is the password complexity for newly created or modified objects/passphrases.
ALTER CERTIFICATE SampleCert1
WITH PRIVATE KEY ( ENCRYPTION BY PASSWORD = '<<new password that complies with password policy>>' );
--returns '000-00-0000' for 'SampleCert1' since it was able to decrypt
select SSN_Encrypted, cast(DecryptByCert(Cert_ID('SampleCert1'),SSN_Encrypted, <<new password that complies with password policy>>') as varchar(12)) as 'SampleCert1'
from Table_1
Or if you prefer to use the DBMK to protect your certificate, you can change your query to the following:
select SSN_Encrypted, cast(DecryptByCert(Cert_ID('SampleCert1'),SSN_Encrypted) as varchar(12)) as 'SampleCert1'
from Table_1
-Raul Garcia
SDE/T
SQL Server Engine
|||Thank you for all your help.
Knowing what password I had to fix made it short work geting it working.
Owen.
Certain rows to excel files
Hello,
I have a table T1 with ID and Desc fields. I want to export this table to excel files based on the ID field. So my stored procedure select all the same T1.ID and export it to ID.xls files. How can I do this?
You can easily achieve this in DTS Import/Export Wizard. Suppose you're using SQL2000, open Enterprise Manager->go to the table from which you want to export data->right click choose All Tasks-> Export Data-> in the Specify Table Copy or Query step, choose Use a Query...->enter the SELECT command (e.g. SELECT * FROM Orders WHERE EmployeeID=6), or use Query Builder->complete the wizard.
|||Thanks for the reply, How should I run the same queries for all the IDs separately in DTS and save the results to excel files?
certain rows to Excel files
Hello,
I have a table T1 with ID and Desc fields. I want to export this table to excel files based on the ID field. So my stored procedure select all the same T1.ID and export it to ID.xls files. How can I do this?
Hi,
While Using DTS Import/Export Wizard,
After Choosing Data Source and DataDestination,
Wizard asks to Specify Table Copy or Query,
At the Time Select the Radio Button [Use a Query to Specify the Data To Transfer].
It will Take you To Type Sql Statement where Query Statement Panel is there ,you can Type Query with your Condition.
|||Thanks for the reply.
This should be done once every day , so I am trying to automate it. How should I develop my query and dts based on this criteria? Any example will be greatly appreciated.
|||Select Jobs From SQL Server Agent Under Management of the Selected Server(In EnterpriseManager)
Select New Job and Name it
Select Steps Tab in New Job Properties
Click New step and Name Step and Write Query in Command Panel
Click New Schedule and Name Schedule and Select Schedule Type
Now you Achieve the Required Thing
|||I still do not see how the query that I am writing will export data to a different excel file based on the ID. All the rows that have the same ID should go to a single excel file, so I am expecting more than one excel file based on the distinct values of ID to be created. How can I do this in DTS?
Certain rows to excel files
I have a table T1 with ID and Desc fields. I want to export this table to
excel files based on the ID field. So my stored procedure select all the sam
e
T1.ID and export it to ID.xls files. How can I do this?Jim,
is this a one-off? If so I'd use the import/export wizard. If it is a
regular process, I'd look at using a scheduled DTS package (you can use the
import/export one as a template for this). There are other options namely
BCP and OSQL which run from the command-line which might also be relevant
for you.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||Hi Paul,
This should be done once every day , so I am trying to automate it. How
should I develop my query and dts based on this criteria? Any example will b
e
greatly appreciated.
"Paul Ibison" wrote:
> Jim,
> is this a one-off? If so I'd use the import/export wizard. If it is a
> regular process, I'd look at using a scheduled DTS package (you can use th
e
> import/export one as a template for this). There are other options namely
> BCP and OSQL which run from the command-line which might also be relevant
> for you.
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
>
>|||Jim,
is the ID list hardcoded or do new IDs appear? If it is hardcoded, then
you'll have a single 'Transform data' task for each ID and the destination
will be also hardcoded in this case.
If this is not the case, then I'd have a lookup table which contains the
ID/path as a pair of columns. You'll need to then iterate through that
table. Each iteration could call the package, sending in the 2 details as
global variables. These global variables will be used to modify the package
to run with the correct ID values. This is done through the Dynamic
Properties task, which would be mapped to the excel file path and the
parameter used for the stored proc.
I realize this sounds complicated, but it's actually quite easy to set up.
The main issue is whether the ID's are known in advance or not. If they are,
then it is quite simple.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||This table has IDs and Desc already in it. IDs might be duplicated and I am
trying to export the rows with the same IDs to a single excel file, so
expecting many excel files based on the distinct values of IDs. DTS seems
quite complicated to me since I have not done anything with that yet.
"Paul Ibison" wrote:
> Jim,
> is the ID list hardcoded or do new IDs appear? If it is hardcoded, then
> you'll have a single 'Transform data' task for each ID and the destination
> will be also hardcoded in this case.
> If this is not the case, then I'd have a lookup table which contains the
> ID/path as a pair of columns. You'll need to then iterate through that
> table. Each iteration could call the package, sending in the 2 details as
> global variables. These global variables will be used to modify the packag
e
> to run with the correct ID values. This is done through the Dynamic
> Properties task, which would be mapped to the excel file path and the
> parameter used for the stored proc.
> I realize this sounds complicated, but it's actually quite easy to set up.
> The main issue is whether the ID's are known in advance or not. If they ar
e,
> then it is quite simple.
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
>
>
>|||JIM.H. wrote:
> This table has IDs and Desc already in it. IDs might be duplicated and I a
m
> trying to export the rows with the same IDs to a single excel file, so
> expecting many excel files based on the distinct values of IDs. DTS seems
> quite complicated to me since I have not done anything with that yet.
>
Jim, here is a link to a message thread that describes how to use a
template XLS file, some VBScript, and OPENROWSET to create a new Excel
document and export data to it, without using DTS. Might prove useful
to you:
http://www.sqlteam.com/forums/topic...926&whichpage=6
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Jim,
if you know all the IDs in advance, then you could hardcode these into the
DTS package as data flow tasks and this would be surprisingly easy (use the
export wizard to create the main template to see how it works), or if you
are more familiar with BCP, you could use a separate bcp commandline for
each excel file.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||Great. Thanks Tracy.
"Tracy McKibben" wrote:
> JIM.H. wrote:
> Jim, here is a link to a message thread that describes how to use a
> template XLS file, some VBScript, and OPENROWSET to create a new Excel
> document and export data to it, without using DTS. Might prove useful
> to you:
> http://www.sqlteam.com/forums/topic...926&whichpage=6
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
>
Certain rows to excel files
I have a table T1 with ID and Desc fields. I want to export this table to
excel files based on the ID field. So my stored procedure select all the same
T1.ID and export it to ID.xls files. How can I do this?Jim,
is this a one-off? If so I'd use the import/export wizard. If it is a
regular process, I'd look at using a scheduled DTS package (you can use the
import/export one as a template for this). There are other options namely
BCP and OSQL which run from the command-line which might also be relevant
for you.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||Hi Paul,
This should be done once every day , so I am trying to automate it. How
should I develop my query and dts based on this criteria? Any example will be
greatly appreciated.
"Paul Ibison" wrote:
> Jim,
> is this a one-off? If so I'd use the import/export wizard. If it is a
> regular process, I'd look at using a scheduled DTS package (you can use the
> import/export one as a template for this). There are other options namely
> BCP and OSQL which run from the command-line which might also be relevant
> for you.
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
>
>|||Jim,
is the ID list hardcoded or do new IDs appear? If it is hardcoded, then
you'll have a single 'Transform data' task for each ID and the destination
will be also hardcoded in this case.
If this is not the case, then I'd have a lookup table which contains the
ID/path as a pair of columns. You'll need to then iterate through that
table. Each iteration could call the package, sending in the 2 details as
global variables. These global variables will be used to modify the package
to run with the correct ID values. This is done through the Dynamic
Properties task, which would be mapped to the excel file path and the
parameter used for the stored proc.
I realize this sounds complicated, but it's actually quite easy to set up.
The main issue is whether the ID's are known in advance or not. If they are,
then it is quite simple.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||This table has IDs and Desc already in it. IDs might be duplicated and I am
trying to export the rows with the same IDs to a single excel file, so
expecting many excel files based on the distinct values of IDs. DTS seems
quite complicated to me since I have not done anything with that yet.
"Paul Ibison" wrote:
> Jim,
> is the ID list hardcoded or do new IDs appear? If it is hardcoded, then
> you'll have a single 'Transform data' task for each ID and the destination
> will be also hardcoded in this case.
> If this is not the case, then I'd have a lookup table which contains the
> ID/path as a pair of columns. You'll need to then iterate through that
> table. Each iteration could call the package, sending in the 2 details as
> global variables. These global variables will be used to modify the package
> to run with the correct ID values. This is done through the Dynamic
> Properties task, which would be mapped to the excel file path and the
> parameter used for the stored proc.
> I realize this sounds complicated, but it's actually quite easy to set up.
> The main issue is whether the ID's are known in advance or not. If they are,
> then it is quite simple.
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
>
>
>|||JIM.H. wrote:
> This table has IDs and Desc already in it. IDs might be duplicated and I am
> trying to export the rows with the same IDs to a single excel file, so
> expecting many excel files based on the distinct values of IDs. DTS seems
> quite complicated to me since I have not done anything with that yet.
>
Jim, here is a link to a message thread that describes how to use a
template XLS file, some VBScript, and OPENROWSET to create a new Excel
document and export data to it, without using DTS. Might prove useful
to you:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926&whichpage=6
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Jim,
if you know all the IDs in advance, then you could hardcode these into the
DTS package as data flow tasks and this would be surprisingly easy (use the
export wizard to create the main template to see how it works), or if you
are more familiar with BCP, you could use a separate bcp commandline for
each excel file.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||Great. Thanks Tracy.
"Tracy McKibben" wrote:
> JIM.H. wrote:
> > This table has IDs and Desc already in it. IDs might be duplicated and I am
> > trying to export the rows with the same IDs to a single excel file, so
> > expecting many excel files based on the distinct values of IDs. DTS seems
> > quite complicated to me since I have not done anything with that yet.
> >
> Jim, here is a link to a message thread that describes how to use a
> template XLS file, some VBScript, and OPENROWSET to create a new Excel
> document and export data to it, without using DTS. Might prove useful
> to you:
> http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926&whichpage=6
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
>
Certain numeric fields not read from the Excel file when using a Excel file source.
I have the Excel Connection Manager and Source to read the contents from an Excel file. For some reason couple of numeric fields from the Excel worksheet are brought over as nulls even though they have a value of 300 and 150. I am not sure why this is happening. I looked into the format of the fields and they are set to General in Excel, I tried setting them to numeric and that did not help.
All the other content from the excel file is coming thru except for the 2 numeric fields.
I tried to bring the contents from the excel source to a text file in csv format and for some reason the 2 numeric fields came out as blank.
Any inputs on getting this addressed will be much appreciated.
Thanks,
Manisha
Try doing a data conversion of this column in the transformation phase using the Derived Column Transformation or the Data Conversion Transformation. Hope that works for you.|||It has to do with a registry setting. I got a response from some other forum and I tried it and it worked.
|||I am having a similar issue. I am importing an Excel sheet from a customer of ours into a SQL Server 2005 table. The first column is text and imports OK. The 2nd column has numbers, but the first two rows import as nulls. If I open the sheet and retype the numbers exactly as they appear, then the sheet imports OK. Each time they send us this sheet I have to retype the numbers in the first two rows. If I import the sheet into Access (using the Access Import wizard), Access does not have a problem and the first two numbers show up OK in Access (without my having to retype them first). How were you able to resolve this issue? Thanks.|||Al C. wrote:
I am having a similar issue. I am importing an Excel sheet from a customer of ours into a SQL Server 2005 table. The first column is text and imports OK. The 2nd column has numbers, but the first two rows import as nulls. If I open the sheet and retype the numbers exactly as they appear, then the sheet imports OK. Each time they send us this sheet I have to retype the numbers in the first two rows. If I import the sheet into Access (using the Access Import wizard), Access does not have a problem and the first two numbers show up OK in Access (without my having to retype them first). How were you able to resolve this issue? Thanks.
Can you set up the Excel connector to import all fields as text fields, and then later convert them to numerics using a derived column transformation?|||
Phil,
TFYR. I verified in the Excel Source (Advanced Editor) that it is using DT_WSTR in both the External columns and Output columns. When I click the Preview button it shows NULL in the first two rows. If I add a DataViewer it also shows nulls. Evidently the first two rows are considered text even though the values are numeric. I can see the difference when I use the formula auditing tool and the first two rows align to the left (indicating text) and the others align to the right (numeric). So what I have gathered is that Excel samples the first nn rows to determine the datatype and since most values are numeric it deems the datatype of the column to be numeric and so it replaces the first two values with nulls. If I modify the Excel connection string and add 'IMEX=1' (import mode=1) to the extended properties, then it works OK.
Certain numeric fields not read from the Excel file when using a Excel file source.
I have the Excel Connection Manager and Source to read the contents from an Excel file. For some reason couple of numeric fields from the Excel worksheet are brought over as nulls even though they have a value of 300 and 150. I am not sure why this is happening. I looked into the format of the fields and they are set to General in Excel, I tried setting them to numeric and that did not help.
All the other content from the excel file is coming thru except for the 2 numeric fields.
I tried to bring the contents from the excel source to a text file in csv format and for some reason the 2 numeric fields came out as blank.
Any inputs on getting this addressed will be much appreciated.
Thanks,
Manisha
Try doing a data conversion of this column in the transformation phase using the Derived Column Transformation or the Data Conversion Transformation. Hope that works for you.|||It has to do with a registry setting. I got a response from some other forum and I tried it and it worked.
|||I am having a similar issue. I am importing an Excel sheet from a customer of ours into a SQL Server 2005 table. The first column is text and imports OK. The 2nd column has numbers, but the first two rows import as nulls. If I open the sheet and retype the numbers exactly as they appear, then the sheet imports OK. Each time they send us this sheet I have to retype the numbers in the first two rows. If I import the sheet into Access (using the Access Import wizard), Access does not have a problem and the first two numbers show up OK in Access (without my having to retype them first). How were you able to resolve this issue? Thanks.|||Al C. wrote:
I am having a similar issue. I am importing an Excel sheet from a customer of ours into a SQL Server 2005 table. The first column is text and imports OK. The 2nd column has numbers, but the first two rows import as nulls. If I open the sheet and retype the numbers exactly as they appear, then the sheet imports OK. Each time they send us this sheet I have to retype the numbers in the first two rows. If I import the sheet into Access (using the Access Import wizard), Access does not have a problem and the first two numbers show up OK in Access (without my having to retype them first). How were you able to resolve this issue? Thanks.
Can you set up the Excel connector to import all fields as text fields, and then later convert them to numerics using a derived column transformation?|||
Phil,
TFYR. I verified in the Excel Source (Advanced Editor) that it is using DT_WSTR in both the External columns and Output columns. When I click the Preview button it shows NULL in the first two rows. If I add a DataViewer it also shows nulls. Evidently the first two rows are considered text even though the values are numeric. I can see the difference when I use the formula auditing tool and the first two rows align to the left (indicating text) and the others align to the right (numeric). So what I have gathered is that Excel samples the first nn rows to determine the datatype and since most values are numeric it deems the datatype of the column to be numeric and so it replaces the first two values with nulls. If I modify the Excel connection string and add 'IMEX=1' (import mode=1) to the extended properties, then it works OK.
Century date conversions
I'm trying to load date fields into SQLServer using DTS, but the
format of the raw data is the number of days since 1 Jan 1900. How do
I convert this to a useful format, is there a standard conversion
routine?
Thanks
Timtim.philbrook@.cazenove.com (Helsop) wrote in message news:<1609a822.0408180233.9fbed33@.posting.google.com>...
> Hi,
> I'm trying to load date fields into SQLServer using DTS, but the
> format of the raw data is the number of days since 1 Jan 1900. How do
> I convert this to a useful format, is there a standard conversion
> routine?
> Thanks
> Tim
There are (at least) two possible solutions. First, load the data into
a staging table and clean it up with TSQL, before an INSERT into the
final table:
insert into dbo.Destination
(col1, datetime_column, ...)
select col1, dateadd(dd, numdays_column, '19000101'), ...
from dbo.Staging
Alternatively, if you want to do the transformation in DTS, then you
could use the VBScript DateAdd() function in an ActiveX column
transformation to achieve the same thing.
Simon|||--> use DATEADD(dd,@.OffsetDays,'1 Jan 1900')
DECLARE @.OffsetDays BIGINT
SET @.OffSetDays = 38217
select DATEADD(dd,@.OffsetDays,'1 Jan 1900')
Tx
Centralizing Two Stored Procedure Queries (one using Cursor Ou
Sorry, one more question: In your example, would there be any way to return
only a subset of the fields in the stored proc query. For example, if you
wanted a temporary table with only the orderid field. Like this:
create table #t (orderid int)
insert into #t(orderid)
exec dbo.p1 @.sd, @.ed
I'll need it like this because my store proc "spTrucker" has many, many
fields, and I only need about 4 or 5 of them in the cursor.
Thanks again,
John
"Alejandro Mesa" wrote:
> John,
> You do not need a cursor output parameter. You can grab the result of the
sp
> "spTrucker" in sp "spDistinctCodes". See "insert into ... exec ..." in BOL
.
> Example:
> use northwind
> go
> create procedure dbo.p1
> @.sd datetime,
> @.ed datetime
> as
> set nocount on
> select
> orderid, orderdate, customerid
> from
> dbo.orders
> where
> orderdate >= convert(char(8), @.sd, 112)
> and orderdate < convert(char(8), dateadd(day, 1, @.ed), 112)
> return @.@.error
> go
> create procedure dbo.p2
> @.sd datetime,
> @.ed datetime
> as
> set nocount on
> declare @.orderid int
> declare @.orderdate varchar(25)
> declare @.customerid nchar(5)
> create table #t (orderid int, orderdate datetime, customerid nchar(5))
> insert into #t(orderid, orderdate, customerid)
> exec dbo.p1 @.sd, @.ed
> declare my_cursor cursor local fast_forward
> for
> select orderid, orderdate, customerid
> from #t
> order by orderdate
> open my_cursor
> while 1 = 1
> begin
> fetch next from my_cursor into @.orderid, @.orderdate, @.customerid
> if @.@.error != 0 or @.@.fetch_status != 0 break
> raiserror('%d %s %s', 10, 1, @.orderid, @.orderdate, @.customerid) with nowa
it
> end
> close my_cursor
> deallocate my_cursor
> go
> exec dbo.p2 '19970701', '19970731'
> go
> drop procedure p2, p1
> go
> How to share data between stored procedures
> http://www.sommarskog.se/share_data.html
>
> AMB
>
> "John Walker" wrote:
>John,
The ddl for the temporary table need to match all columns returned by the
sp. You can not grab just a subset.
AMB
"John Walker" wrote:
> Alejandro,
> Sorry, one more question: In your example, would there be any way to retu
rn
> only a subset of the fields in the stored proc query. For example, if you
> wanted a temporary table with only the orderid field. Like this:
> create table #t (orderid int)
> insert into #t(orderid)
> exec dbo.p1 @.sd, @.ed
> I'll need it like this because my store proc "spTrucker" has many, many
> fields, and I only need about 4 or 5 of them in the cursor.
> Thanks again,
> John
> "Alejandro Mesa" wrote:
>
Sunday, February 12, 2012
Cast question
'
for example. I've been successful in doing this with LIKE 'Debug' or LIKE
'Debug%'
however, I'd like to do an = comparison. Some fields are varchar, some are
text. The text ones seem to be a problem.
Is there a way to do a Cast, so that I can do an = comparison with these
types of fields?>> Is there a way to do a Cast, so that I can do an = comparison with these
If the length of the TEXT values are less than 8000, you can cast them to
VARCHAR type.
Anith
Friday, February 10, 2012
cast datetime to yyyymm format
firstdate and seconddate are both datetime fields and I want to use them in
the yyyymm format in my where clause below.
Declare @.curdate datetime
Set @.curdate = 2005/11
select firstdate, seconddate
from dates
where @.curdate between firstdate and seconddate
and firstdate < seconddate
Thanks!
MattI do not know any style argument to format it like that, but you can buid it
yourself in a function
DECLARE @.TestDate datetime
SET @.TestDate = GetDate()
SELECT CONVERT(varchar(4), @.TestDate, 120) + SUBSTRING(CONVERT(varchar(7),
@.TestDate, 120), 6, 2)
You can buid a function that takes a datetime argument and returns a varchar
.
HTH,
John Scragg
"Matt" wrote:
> How can I cast a datetime to a format of yyyymm?
> firstdate and seconddate are both datetime fields and I want to use them i
n
> the yyyymm format in my where clause below.
> Declare @.curdate datetime
> Set @.curdate = 2005/11
> select firstdate, seconddate
> from dates
> where @.curdate between firstdate and seconddate
> and firstdate < seconddate
> Thanks!
> Matt|||Try,
select convert(char(6), getdate(), 112)
go
AMB
"Matt" wrote:
> How can I cast a datetime to a format of yyyymm?
> firstdate and seconddate are both datetime fields and I want to use them i
n
> the yyyymm format in my where clause below.
> Declare @.curdate datetime
> Set @.curdate = 2005/11
> select firstdate, seconddate
> from dates
> where @.curdate between firstdate and seconddate
> and firstdate < seconddate
> Thanks!
> Matt|||2005/11 is not a date. A date requires year, month and day.
e.g., to find dates in Nov 2005,
set @.curdate='20051101'
select firstdate, seconddate
from dates
where firstdate<=@.curdate and seconddate>=dateadd(month,1,@.curdate)-1
and firstdate<seconddate
Matt wrote:
>How can I cast a datetime to a format of yyyymm?
>firstdate and seconddate are both datetime fields and I want to use them in
>the yyyymm format in my where clause below.
>Declare @.curdate datetime
>Set @.curdate = 2005/11
> select firstdate, seconddate
>from dates
>where @.curdate between firstdate and seconddate
> and firstdate < seconddate
>Thanks!
>Matt
>|||Thanks to you all for your help. It does appear that I get correct results
when I run the @.curdate as a char. When running as datetime the where claus
e
fails.
"Matt" wrote:
> How can I cast a datetime to a format of yyyymm?
> firstdate and seconddate are both datetime fields and I want to use them i
n
> the yyyymm format in my where clause below.
> Declare @.curdate datetime
> Set @.curdate = 2005/11
> select firstdate, seconddate
> from dates
> where @.curdate between firstdate and seconddate
> and firstdate < seconddate
> Thanks!
> Matt|||On Wed, 26 Oct 2005 12:41:18 -0700, Matt wrote:
>Thanks to you all for your help. It does appear that I get correct results
>when I run the @.curdate as a char. When running as datetime the where clau
se
>fails.
Hi Matt,
As Trey already wrote: 2005/11 is not a datetime. It is the division of
an integer by another integer. The result of 2005/11 is 182 (the decimal
portion is lost, because integer division is used). This result is then
converted to datetime, which will yield the 182nd day of the year 1900.
To check this, run
Declare @.curdate datetime
Set @.curdate = 2005/11
SELECT @.curdate
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
Casing
This is a real problem when mixing and matching.
For example, C uses lower case for everything, but when calling .Net
functions - you have to use Pascal Casing (1st Character of all words
Capitalized). In VB.Net, you use Pascal style for everthing but variables
where you use Camel Casing (1st Character of all ll words capitalized,
except the 1st word).
I tend to use Microsofts guidelines where everything but variables are
Pascal. Variables are Camel Casing. I never use and never liked Hungarian
(prefixed with variable type).
Celko mentioned the problem with Camel Casing being harder to read.
I have seen various ways of casing in Sql Server.
I have also seen various opinions on whether to pluralize table names
(Employees table instead of Employee table). I tend to pluralize myself,
but have seen guidlines that suggest it is better to use the singular form.
I see Sql statements both as SELECT and select, but haven't really seen any
definitive agreement on this.
Much of this just seems to be personal preference - which would be a real
problem in C as case is important.
Just curious.
Thanks,
TomSee my rules in SQL PROGRAMMING STYLE. They are based on ISO-11179,
human factors and readability research. It is a mater of a few decades
of research, not personal taste.
1) For table names, in order of preference
industry standard name - best
collective nouns (Personnel) -better
plural nouns (Employees) - bad
singluar (employee) - not usable, less the set has just one member
Capitalize it, as you would in English, German, etc. for proper names.
2) uppercase reserved words because they are read as a Bouma (single
unit of eye scan). The compiler will take care of typos.
3) lowercase scalars because they are longer and this easier to read
and less likely to be misspelt. Never use "camelCase" because the eye
jumps to the capital letter, then back to the start of the word.
The reason C is a "lowercase" language actually has to do with
teletypes! It is a physical effort to hold down the shift key and when
you are a two-finger typists, you don't want to write anything very
long.|||"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1125333962.715849.259610@.g47g2000cwa.googlegroups.com...
> See my rules in SQL PROGRAMMING STYLE. They are based on ISO-11179,
> human factors and readability research. It is a mater of a few decades
> of research, not personal taste.
> 1) For table names, in order of preference
> industry standard name - best
> collective nouns (Personnel) -better
> plural nouns (Employees) - bad
> singluar (employee) - not usable, less the set has just one member
> Capitalize it, as you would in English, German, etc. for proper names.
I assume you are speaking Pascal casing here.
>
> 2) uppercase reserved words because they are read as a Bouma (single
> unit of eye scan). The compiler will take care of typos.
> 3) lowercase scalars because they are longer and this easier to read
> and less likely to be misspelt. Never use "camelCase" because the eye
> jumps to the capital letter, then back to the start of the word.
What about Pascal style for variables (fields), scalars?
CamelCase is used quite a bit and is the what was used for Hungarian
(although it has been pretty much being phased out of MS notation).
> The reason C is a "lowercase" language actually has to do with
> teletypes! It is a physical effort to hold down the shift key and when
> you are a two-finger typists, you don't want to write anything very
> long.
>
True.
Thanks,
Tom