Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Monday, March 26, 2012

Multi Format Text File

Hello. I am in the process of migrating an old app to SQL Server. The old app reads hundreds of different flat file formats. One of the more complex ones is a multi-format delimited file. For example:

01^Bob Johnson^123 Main St^Anytown^St
02^Book1^$20
02^Book2^$30
03^Gift Cert^Happy Birthday^$100

This file is delimited with the ^ character. Note that the first 2 characters identify the row type. All 01 rows have data in the format: Name, Street Address, City, State. All 02 have data in the format: Book name, price. Etc.

Any clever ideas on how to parse this? I tried setting it up as a flat file source with the ^ delimiter. It doesn't work - in this example it wraps the third row to the end of the second row and keeps adding columns to fill out the row.

The only option that I can think of is to pull the entire row into one long column, and then use a script component to manually substring each column out.

Any help would be greatly appreciated.

Thanks,
Chris

tofferr wrote:

The only option that I can think of is to pull the entire row into one long column, and then use a script component to manually substring each column out.

Yes! That's the best thing to do, in my opinion, and what I do in practice. You can also use a derived column to substring the rows instead of a script component.

Also, you could have one data flow with a conditional split in it to direct each record type to a unique flat file, raw file, sql server table, etc... for staging. From there you can have have an appropriately mapped flat file connection (to the staging file/table/whatever) and continue using a data flow as you normally would.

So, bottom line, which would you rather maintain? substrings or flat file definitions?|||It looks like the rows are related. I think the script will need to generate some type of transaction key for every 01 record and carry it forward for the other row types. I don't think the relationship between the rows can be maintained by using the alternate methods.|||

JayH wrote:

It looks like the rows are related. I think the script will need to generate some type of transaction key for every 01 record and carry it forward for the other row types. I don't think the relationship between the rows can be maintained by using the alternate methods.

If each file only contains one name, then you're fine. If if contains more than one name then when loading the initial file you may want to use a script component to generate a unique key which gets incremented by one each time you read a new 01 record. Carry this key forward and you can later reassemble the records accordingly.|||Thank you for the quick responses!

I simplified my example significantly. For example, a 01 row really has well over 150 fields. And, yes, the rows are related and there are many 01 records in each file, so I believe Jay H is correct.and the alternate methods will not work.

Any other helpful thoughts?

BTW, I tried to check out http://www.ssistalk.com/ but it did not load? Temporary problem?

Thanks,
Chris
|||

tofferr wrote:

Any other helpful thoughts?


Yep, see my response above. You can mark each "record" (the 01 and its associated children) with a unique key. Then later when you work with the data you can work on batches as defined by that key.

tofferr wrote:


BTW, I tried to check out http://www.ssistalk.com/ but it did not load? Temporary problem?

Not sure what the deal was there, but the machine was unresponsive. It's back up. Thanks for the note.

Friday, March 23, 2012

Mulit MDF Files Seperately Restore On SQL Server 2000

Hi there,
I have a question about backup restore. I have setup a database with 5 MDF
file located in different drive.
testdb_data1.MDF
testdb_data2.MDF (in USB drive)
testdb_data3.MDF
testdb_data4.MDF
testdb_data5.MDF
If the MDF file in USB drive fail (say I unplug it and replace with a new
USB drive), is that possible to restore this MDF file only in order to
recover the whole database? I have tried to backup all the MDF files but
fail to restore in this case.
Could anyone give me some hints on this question? Thanks in advance.
KennethAre you backup up the database files from Windows or using SQL Servers BACKUP command?
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Black" <hoodworld@.hotmail.com> wrote in message news:uTvjY90yHHA.484@.TK2MSFTNGP06.phx.gbl...
> Hi there,
> I have a question about backup restore. I have setup a database with 5 MDF
> file located in different drive.
> testdb_data1.MDF
> testdb_data2.MDF (in USB drive)
> testdb_data3.MDF
> testdb_data4.MDF
> testdb_data5.MDF
> If the MDF file in USB drive fail (say I unplug it and replace with a new
> USB drive), is that possible to restore this MDF file only in order to
> recover the whole database? I have tried to backup all the MDF files but
> fail to restore in this case.
> Could anyone give me some hints on this question? Thanks in advance.
> Kenneth
>|||And are they in same file group? On which version of sql?
"Tibor Karaszi" wrote:
> Are you backup up the database files from Windows or using SQL Servers BACKUP command?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://sqlblog.com/blogs/tibor_karaszi
>
> "Black" <hoodworld@.hotmail.com> wrote in message news:uTvjY90yHHA.484@.TK2MSFTNGP06.phx.gbl...
> > Hi there,
> >
> > I have a question about backup restore. I have setup a database with 5 MDF
> > file located in different drive.
> >
> > testdb_data1.MDF
> > testdb_data2.MDF (in USB drive)
> > testdb_data3.MDF
> > testdb_data4.MDF
> > testdb_data5.MDF
> >
> > If the MDF file in USB drive fail (say I unplug it and replace with a new
> > USB drive), is that possible to restore this MDF file only in order to
> > recover the whole database? I have tried to backup all the MDF files but
> > fail to restore in this case.
> >
> > Could anyone give me some hints on this question? Thanks in advance.
> >
> > Kenneth
> >
> >|||In sql2k, you can restore single file with sql backup/restore. But need to do
log restores to keep data integrity.
"Tibor Karaszi" wrote:
> Are you backup up the database files from Windows or using SQL Servers BACKUP command?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://sqlblog.com/blogs/tibor_karaszi
>
> "Black" <hoodworld@.hotmail.com> wrote in message news:uTvjY90yHHA.484@.TK2MSFTNGP06.phx.gbl...
> > Hi there,
> >
> > I have a question about backup restore. I have setup a database with 5 MDF
> > file located in different drive.
> >
> > testdb_data1.MDF
> > testdb_data2.MDF (in USB drive)
> > testdb_data3.MDF
> > testdb_data4.MDF
> > testdb_data5.MDF
> >
> > If the MDF file in USB drive fail (say I unplug it and replace with a new
> > USB drive), is that possible to restore this MDF file only in order to
> > recover the whole database? I have tried to backup all the MDF files but
> > fail to restore in this case.
> >
> > Could anyone give me some hints on this question? Thanks in advance.
> >
> > Kenneth
> >
> >

Monday, March 19, 2012

MSSQLSERVER service terminated unexpectedly

HI
I have server MSDE 2000 SP3
frequently server stopped, and print in log file information:
1)
17310 :
SqlDumpExceptionHandler: Process 1984 generated fatal exception c0000005
EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
2)
17311 :
SQL Server is aborting. Fatal exception c0000005 caught.
3)
The MSSQLSERVER service terminated unexpectedly.
If you didn't find anything in KB on this and you are on the current service pack: Open a case with MS
Support. Exceptions etc are most often bugs in SQL Server, so you will probably need a fix or workaround from
MS for this.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Bies" <ANTYSPAMbies22@.poczta.onet.pl> wrote in message news:ca1ij7$dd6$1@.atlantis.news.tpi.pl...
> HI
> I have server MSDE 2000 SP3
> frequently server stopped, and print in log file information:
> 1)
> 17310 :
> SqlDumpExceptionHandler: Process 1984 generated fatal exception c0000005
> EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
> 2)
> 17311 :
> SQL Server is aborting. Fatal exception c0000005 caught.
> 3)
> The MSSQLSERVER service terminated unexpectedly.
>

MSSQLSERVER service terminated unexpectedly

HI
I have server MSDE 2000 SP3
frequently server stopped, and print in log file information:

1)
17310 :
SqlDumpExceptionHandler: Process 1984 generated fatal exception c0000005
EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.

2)
17311 :
SQL Server is aborting. Fatal exception c0000005 caught.

3)
The MSSQLSERVER service terminated unexpectedly.Hi

This may be as in:

http://support.microsoft.com/defaul...kb;en-us;814572

other possibilities
http://search.microsoft.com/search/...ql+server&na=54

You may also want to check if a anti-virus software is affecting it.

John

"Bies" <ANTYSPAMbies22@.poczta.onet.pl> wrote in message
news:ca1ie6$6gh$1@.nemesis.news.tpi.pl...
> HI
> I have server MSDE 2000 SP3
> frequently server stopped, and print in log file information:
> 1)
> 17310 :
> SqlDumpExceptionHandler: Process 1984 generated fatal exception c0000005
> EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
> 2)
> 17311 :
> SQL Server is aborting. Fatal exception c0000005 caught.
> 3)
> The MSSQLSERVER service terminated unexpectedly.|||> This may be as in:
> http://support.microsoft.com/defaul...kb;en-us;814572
> other possibilities
http://search.microsoft.com/search/...ql+server&na=54
> You may also want to check if a anti-virus software is affecting it.

thx

MSSQLSERVER service terminated unexpectedly

HI
I have server MSDE 2000 SP3
frequently server stopped, and print in log file information:
1)
17310 :
SqlDumpExceptionHandler: Process 1984 generated fatal exception c0000005
EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
2)
17311 :
SQL Server is aborting. Fatal exception c0000005 caught.
3)
The MSSQLSERVER service terminated unexpectedly.If you didn't find anything in KB on this and you are on the current service
pack: Open a case with MS
Support. Exceptions etc are most often bugs in SQL Server, so you will proba
bly need a fix or workaround from
MS for this.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Bies" <ANTYSPAMbies22@.poczta.onet.pl> wrote in message news:ca1ij7$dd6$1@.atlantis.news.tpi.
pl...
> HI
> I have server MSDE 2000 SP3
> frequently server stopped, and print in log file information:
> 1)
> 17310 :
> SqlDumpExceptionHandler: Process 1984 generated fatal exception c0000005
> EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
> 2)
> 17311 :
> SQL Server is aborting. Fatal exception c0000005 caught.
> 3)
> The MSSQLSERVER service terminated unexpectedly.
>

MSSQLSERVER service terminated unexpectedly

HI
I have server MSDE 2000 SP3
frequently server stopped, and print in log file information:
1)
17310 :
SqlDumpExceptionHandler: Process 1984 generated fatal exception c0000005
EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
2)
17311 :
SQL Server is aborting. Fatal exception c0000005 caught.
3)
The MSSQLSERVER service terminated unexpectedly.If you didn't find anything in KB on this and you are on the current service pack: Open a case with MS
Support. Exceptions etc are most often bugs in SQL Server, so you will probably need a fix or workaround from
MS for this.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Bies" <ANTYSPAMbies22@.poczta.onet.pl> wrote in message news:ca1ij7$dd6$1@.atlantis.news.tpi.pl...
> HI
> I have server MSDE 2000 SP3
> frequently server stopped, and print in log file information:
> 1)
> 17310 :
> SqlDumpExceptionHandler: Process 1984 generated fatal exception c0000005
> EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
> 2)
> 17311 :
> SQL Server is aborting. Fatal exception c0000005 caught.
> 3)
> The MSSQLSERVER service terminated unexpectedly.
>

Monday, March 12, 2012

MSSQLServer Service Account

Hi,
We have an automated process which imports a file from a
remote server. Unfortunately this requires the
MSSQLServer service to be running under a domain account
in order to be able to access the remote server. I have
been unable to get SQL Server to start with anything other
than Domain Admin rights.
Do you know what rights are required for the MSSQLServer
service to run?
Thanks,
Phil.Hi,
Requirements for Domain User Account
All domain user accounts must have permission to:
Access and change the SQL Server directory (\Program Files\Microsoft SQL
Server\Mssql).
Access and change the .mdf, .ndf, and .ldf database files.
Log on as a service.
Read and write registry keys at and under:
HKEY_LOCAL_MACHINE\Software\Microsoft\MS
SQLServer.
-or- for any named instance service:
HKEY_LOCAL_MACHINE\Software\Microsoft\Mi
crosoft SQL Server.
HKEY_LOCAL_MACHINE\System\CurrentControl
set\Services\MSSQLServer.
-or- for any named instance:
HKEY_LOCAL_MACHINE\System\CurrentControl
set\Services\MSSQL$Instancename.
HKEY_LOCAL_MACHINE\Software\Microsoft\Wi
ndows NT\CurrentVersion\Perflib.
In addition, a domain user account must be able to read and write
corresponding registry keys for these services: SQLAgent$InstanceName,
MSSearch, and MSDTC.
SQL Server startup user should be:-
Member of local Power Users or local Administrators group
SQL Server Agent should be :-
Member of the Administrators local group
Create CmdExec and ActiveScript jobs belonging to someone other than a SQL
Server administrator.
Thanks
Hari
MCDBA
"Phil" <anonymous@.discussions.microsoft.com> wrote in message
news:2273a01c45cfd$6a7f85c0$a001280a@.phx
.gbl...
> Hi,
> We have an automated process which imports a file from a
> remote server. Unfortunately this requires the
> MSSQLServer service to be running under a domain account
> in order to be able to access the remote server. I have
> been unable to get SQL Server to start with anything other
> than Domain Admin rights.
> Do you know what rights are required for the MSSQLServer
> service to run?
> Thanks,
> Phil.|||If you change the service accounts using Enterprise Manager,
the rights and permissions are handled for you. Otherwise
you need to manage these manually and follow this article:
HOW TO: Change the SQL Server or SQL Server Agent Service
Account Without Using SQL Enterprise Manager in SQL Server
2000
http://support.microsoft.com/?id=283811
-Sue
On Mon, 28 Jun 2004 03:48:15 -0700, "Phil"
<anonymous@.discussions.microsoft.com> wrote:

>Hi,
>We have an automated process which imports a file from a
>remote server. Unfortunately this requires the
>MSSQLServer service to be running under a domain account
>in order to be able to access the remote server. I have
>been unable to get SQL Server to start with anything other
>than Domain Admin rights.
>Do you know what rights are required for the MSSQLServer
>service to run?
>Thanks,
>Phil.

Friday, March 9, 2012

MSSQL7

I am a beginner with MSSQL7 so please bear with me. I am trying to open a
database that is approx 187KB in size. This file resides in the following
folder; C:\MSSQL7\DATA. This was restored from a backup we reterived from a
subjects computer. I obtained a copy of the MSSQL7 software. I was able to
load it on a standalone computer. This is as far as I can get. I want to be
able to copy the 187KB file in the approporiate folder of this newley
installed MSSQL7 program so I can simply open up the tables and see the data.
When I start Enterprise manager, I can't seem to connect it to this old
database.
Please help
cart34@.netzero.net
Have a look at these. You can use sp_attach_db to add it to the new server.
http://www.support.microsoft.com/?id=314546 Moving DB's between Servers
http://www.support.microsoft.com/?id=224071 Moving SQL Server Databases
to a New Location with Detach/Attach
http://support.microsoft.com/?id=221465 Using WITH MOVE in a
Restore
http://www.support.microsoft.com/?id=246133 How To Transfer Logins and
Passwords Between SQL Servers
http://www.support.microsoft.com/?id=298897 Mapping Logins & SIDs after a
Restore
http://www.dbmaint.com/SyncSqlLogins.asp Utility to map logins to
users
http://www.support.microsoft.com/?id=168001 User Logon and/or Permission
Errors After Restoring Dump
http://www.support.microsoft.com/?id=240872 How to Resolve Permission
Issues When a Database Is Moved Between SQL Servers
http://www.sqlservercentral.com/scri...p?scriptid=599
Restoring a .mdf
http://www.support.microsoft.com/?id=307775 Disaster Recovery Articles
for SQL Server
Andrew J. Kelly SQL MVP
"Cart34" <Cart34@.discussions.microsoft.com> wrote in message
news:522E836E-979C-4D80-8823-949D3148A6A9@.microsoft.com...
>I am a beginner with MSSQL7 so please bear with me. I am trying to open a
> database that is approx 187KB in size. This file resides in the following
> folder; C:\MSSQL7\DATA. This was restored from a backup we reterived from
> a
> subjects computer. I obtained a copy of the MSSQL7 software. I was able
> to
> load it on a standalone computer. This is as far as I can get. I want to
> be
> able to copy the 187KB file in the approporiate folder of this newley
> installed MSSQL7 program so I can simply open up the tables and see the
> data.
> When I start Enterprise manager, I can't seem to connect it to this old
> database.
> Please help
> cart34@.netzero.net

MSsQL2005; OPENROWSET, BLOB/IMAGE and STORED PROCEDURE problems

All,

I work with Microsoft SQL Server 2005 on windows XP professional.
I'd like to create stored procdure to add image to my database (jpg file).
I managed to do it using VARCHAR variable in stored procedure
and then using EXEC, but it don't work directly.

My Table definiton:
CREATE TABLE [dbo].[Users](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[Login] [char](10),
[Password] [char](20),
[Avatar] [image] NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[UserID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

My working solution using stored procedure:
ALTER PROCEDURE [dbo].[AddUser]
@.Login AS VARCHAR(255),
@.Password AS VARCHAR(255),
@.AvatarFileLocation AS VARCHAR(255),
@.UserId AS INT OUTPUT
AS
BEGIN
SET @.Query = 'INSERT INTO USERS ' + CHAR(13)
+ 'SELECT '''+ @.Login + ''' AS Login, ' + CHAR(13)
+ '''' + @.Password + ''' AS Password,' + CHAR(13)
+ '(SELECT * FROM OPENROWSET(BULK ''' + @.AvatarFileLocation + ''', SINGLE_BLOB) AS OBRAZEK)'
EXECUTE (@.Query)
SET @.UserID = @.@.IDENTITY
END

I'd like to use statement in the stored procdure:
ALTER PROCEDURE [dbo].[AddUser]
@.Login AS VARCHAR(255),
@.Password AS VARCHAR(255),
@.AvatarFileLocation AS VARCHAR(255),
@.UserId AS INT OUTPUT
AS
BEGIN
DECLARE
@.Query AS VARCHAR(MAX)

SET @.AvatarFileLocation = 'C:\hitman1.jpg'
INSERT INTO USERS
SELECT @.Login AS Login,
@.Password AS Password,
(SELECT * FROM OPENROWSET(BULK @.AvatarFileLocation, SINGLE_BLOB) AS OBRAZEK)


SET @.UserID = @.@.IDENTITY

END


It generates error:
Incorrect syntax near '@.AvatarFileLocation'.

My question is:
Why it does not work and how to write the stored procedure code to run this code without errors.

Thanks for any reply

You can't use a variable inside OPENROWSET.

What you are doing, in any case, IS VERY DANGEROUS. There

are many ways in which dynamic SQL is vulnerable to SQL

injection. Please read about it, so that you don't lose

everything you have when a malicious user joins your

site/forum with a password like

O',0x; delete from USERS where Password <> 'O';return 0;--

You might start reading here:

http://www.sommarskog.se/dynamic_sql.html

Steve Kass

Drew University

Michal1979@.discussions.microsoft.com wrote:

> All,

>

> I work with Microsoft SQL Server 2005 on windows XP professional.

> I'd like to create stored procdure to add image to my database (jpg

> file).

> I managed to do it using VARCHAR variable in stored procedure

> and then using EXEC, but it don't work directly.

>

> My Table definiton:

> CREATE TABLE [dbo].[Users](

> [UserID] [int] IDENTITY(1,1) NOT NULL,

> [Login] [char](10),

> [Password] [char](20),

> [Avatar] [image] NULL,

> CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED

> (

> [UserID] ASC

> )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

> ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

>

> My working solution using stored procedure:

> ALTER PROCEDURE [dbo].[AddUser]

> @.Login AS VARCHAR(255),

> @.Password AS VARCHAR(255),

> @.AvatarFileLocation AS VARCHAR(255),

> @.UserId AS INT OUTPUT

> AS

> BEGIN

> SET @.Query = 'INSERT INTO USERS ' + CHAR(13)

> + 'SELECT '''+ @.Login + ''' AS Login, ' + CHAR(13)

> + '''' + @.Password + ''' AS Password,' + CHAR(13)

> + '(SELECT * FROM OPENROWSET(BULK ''' + @.AvatarFileLocation + ''',

> SINGLE_BLOB) AS OBRAZEK)'

> EXECUTE (@.Query)

> SET @.UserID = @.@.IDENTITY

> END

>

> I'd like to use statement in the stored procdure:

> ALTER PROCEDURE [dbo].[AddUser]

> @.Login AS VARCHAR(255),

> @.Password AS VARCHAR(255),

> @.AvatarFileLocation AS VARCHAR(255),

> @.UserId AS INT OUTPUT

> AS

> BEGIN

> DECLARE

> @.Query AS VARCHAR(MAX)

>

> SET @.AvatarFileLocation = 'C:\hitman1.jpg'

> INSERT INTO USERS

> SELECT @.Login AS Login,

> @.Password AS Password,

> (SELECT * FROM OPENROWSET(BULK @.AvatarFileLocation, SINGLE_BLOB) AS

> OBRAZEK)

>

>

> SET @.UserID = @.@.IDENTITY

>

> END

>

>

> It generates error:

> Incorrect syntax near '@.AvatarFileLocation'.

>

> My question is:

> Why it does not work and how to write the stored procedure code to run

> this code without errors.

>

> Thanks for any reply

>

>

>

>|||

NNTP User

Lot thanks for the repy,

The article link is very interesting.

I understand risks from using dynamic sql, but I'm not goiing to allow user call my stored procedure directly.

All arguments of the stored procedure will be validated and constructed my my application.

My real question is why can I use OPENROWSET function in stored procedure when I build the query

using varchar variable and then execute it, although I can't do it direclty using OPENROWSET in

stored procedure.

|||You cannot use a variable like this:

OPENROWSET(BULK @.AvatarFileLocation, SINGLE_BLOB)

The file name must be hard-coded, like this:

OPENROWSET(BULK 'C:\picture.jpg', SINGLE_BLOB)

SK

Michal1979@.discussions.microsoft.com wrote:

> NNTP User

>

>

> Lot thanks for the repy,

>

> The article link is very interesting.

>

> I understand risks from using dynamic sql, but I'm not goiing to allow

> user call my stored procedure directly.

>

> All arguments of the stored procedure will be validated and constructed

> my my application.

>

> My real question is why can I use OPENROWSET function in stored

> procedure when I build the query

>

> using varchar variable and then execute it, although I can't do it

> direclty using OPENROWSET in

>

> stored procedure.

>

>

>

>

>

>|||

NNTP User,

Yes,I can not use directly OPENROWSET function in strored procedure, but I can use dynamic sql.

My problem is not how to store image using stored procedure (I can do it, however I don't like the way I do it)

but why can't I do it directly.

By the way I tried to use sp_executesql to run the code but it didn't work as well - so for now the only way

to store image or file using stored procedure is dynamic sql.

But I'm still wandering WHY? Is it a bug or something like that?

|||

As Steve indicated, you will have to specify the filename as a literal in the OPENROWSET call. Otherwise you will have to use dynamic SQL to form the entire statement and execute it. And with dynamic SQL you will have protect against SQL injection attacks. Optionally, you can do the following for a bulk import process:

1. Create temporary table to hold the user accounts

2. Use the new BulkCopy managed API to stream the user data from the client to server

3. Write SP to dump the rows from temporary table to the main table

For adding or modifying single values, you can just have a SP with image parameter and manipulate the data in the table. There is no need to use OPENROWSET which requires a file (creation of file by client, server having permissions to access file, etc) among other things and parameterization is not straight-forward.

|||

Umachandar Jayachandran - MS,

I'd like to know if there is other than OPENROWSET form of loading images or binary files into MSSQL,

I could missed it in documentation. It is sure that there will be no possibility to sql injection using dynamic sql

in my case.

Probably I should mentioned it before, the problem I described is not critical for me, becouse I found solution.

The solution may not be ideal (especialy for me) but it still works. Anyway probably I'll use C# and ADO.NET to

perform put and get image from database.

Maybe you know why the only way to use OPENROWSET in stored procedure is to use dynamic sql.

Thanks

MSsQL2005; OPENROWSET, BLOB/IMAGE and STORED PROCEDURE problems

All,

I work with Microsoft SQL Server 2005 on windows XP professional.
I'd like to create stored procdure to add image to my database (jpg file).
I managed to do it using VARCHAR variable in stored procedure
and then using EXEC, but it don't work directly.

My Table definiton:
CREATE TABLE [dbo].[Users](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[Login] [char](10),
[Password] [char](20),
[Avatar] [image] NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[UserID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

My working solution using stored procedure:
ALTER PROCEDURE [dbo].[AddUser]
@.Login AS VARCHAR(255),
@.Password AS VARCHAR(255),
@.AvatarFileLocation AS VARCHAR(255),
@.UserId AS INT OUTPUT
AS
BEGIN
SET @.Query = 'INSERT INTO USERS ' + CHAR(13)
+ 'SELECT '''+ @.Login + ''' AS Login, ' + CHAR(13)
+ '''' + @.Password + ''' AS Password,' + CHAR(13)
+ '(SELECT * FROM OPENROWSET(BULK ''' + @.AvatarFileLocation + ''', SINGLE_BLOB) AS OBRAZEK)'
EXECUTE (@.Query)
SET @.UserID = @.@.IDENTITY
END

I'd like to use statement in the stored procdure:
ALTER PROCEDURE [dbo].[AddUser]
@.Login AS VARCHAR(255),
@.Password AS VARCHAR(255),
@.AvatarFileLocation AS VARCHAR(255),
@.UserId AS INT OUTPUT
AS
BEGIN
DECLARE
@.Query AS VARCHAR(MAX)

SET @.AvatarFileLocation = 'C:\hitman1.jpg'
INSERT INTO USERS
SELECT @.Login AS Login,
@.Password AS Password,
(SELECT * FROM OPENROWSET(BULK @.AvatarFileLocation, SINGLE_BLOB) AS OBRAZEK)


SET @.UserID = @.@.IDENTITY

END


It generates error:
Incorrect syntax near '@.AvatarFileLocation'.

My question is:
Why it does not work and how to write the stored procedure code to run this code without errors.

Thanks for any reply

You can't use a variable inside OPENROWSET.

What you are doing, in any case, IS VERY DANGEROUS. There

are many ways in which dynamic SQL is vulnerable to SQL

injection. Please read about it, so that you don't lose

everything you have when a malicious user joins your

site/forum with a password like

O',0x; delete from USERS where Password <> 'O';return 0;--

You might start reading here:

http://www.sommarskog.se/dynamic_sql.html

Steve Kass

Drew University

Michal1979@.discussions.microsoft.com wrote:

> All,

>

> I work with Microsoft SQL Server 2005 on windows XP professional.

> I'd like to create stored procdure to add image to my database (jpg

> file).

> I managed to do it using VARCHAR variable in stored procedure

> and then using EXEC, but it don't work directly.

>

> My Table definiton:

> CREATE TABLE [dbo].[Users](

> [UserID] [int] IDENTITY(1,1) NOT NULL,

> [Login] [char](10),

> [Password] [char](20),

> [Avatar] [image] NULL,

> CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED

> (

> [UserID] ASC

> )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

> ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

>

> My working solution using stored procedure:

> ALTER PROCEDURE [dbo].[AddUser]

> @.Login AS VARCHAR(255),

> @.Password AS VARCHAR(255),

> @.AvatarFileLocation AS VARCHAR(255),

> @.UserId AS INT OUTPUT

> AS

> BEGIN

> SET @.Query = 'INSERT INTO USERS ' + CHAR(13)

> + 'SELECT '''+ @.Login + ''' AS Login, ' + CHAR(13)

> + '''' + @.Password + ''' AS Password,' + CHAR(13)

> + '(SELECT * FROM OPENROWSET(BULK ''' + @.AvatarFileLocation + ''',

> SINGLE_BLOB) AS OBRAZEK)'

> EXECUTE (@.Query)

> SET @.UserID = @.@.IDENTITY

> END

>

> I'd like to use statement in the stored procdure:

> ALTER PROCEDURE [dbo].[AddUser]

> @.Login AS VARCHAR(255),

> @.Password AS VARCHAR(255),

> @.AvatarFileLocation AS VARCHAR(255),

> @.UserId AS INT OUTPUT

> AS

> BEGIN

> DECLARE

> @.Query AS VARCHAR(MAX)

>

> SET @.AvatarFileLocation = 'C:\hitman1.jpg'

> INSERT INTO USERS

> SELECT @.Login AS Login,

> @.Password AS Password,

> (SELECT * FROM OPENROWSET(BULK @.AvatarFileLocation, SINGLE_BLOB) AS

> OBRAZEK)

>

>

> SET @.UserID = @.@.IDENTITY

>

> END

>

>

> It generates error:

> Incorrect syntax near '@.AvatarFileLocation'.

>

> My question is:

> Why it does not work and how to write the stored procedure code to run

> this code without errors.

>

> Thanks for any reply

>

>

>

>|||

NNTP User

Lot thanks for the repy,

The article link is very interesting.

I understand risks from using dynamic sql, but I'm not goiing to allow user call my stored procedure directly.

All arguments of the stored procedure will be validated and constructed my my application.

My real question is why can I use OPENROWSET function in stored procedure when I build the query

using varchar variable and then execute it, although I can't do it direclty using OPENROWSET in

stored procedure.

|||You cannot use a variable like this:

OPENROWSET(BULK @.AvatarFileLocation, SINGLE_BLOB)

The file name must be hard-coded, like this:

OPENROWSET(BULK 'C:\picture.jpg', SINGLE_BLOB)

SK

Michal1979@.discussions.microsoft.com wrote:

> NNTP User

>

>

> Lot thanks for the repy,

>

> The article link is very interesting.

>

> I understand risks from using dynamic sql, but I'm not goiing to allow

> user call my stored procedure directly.

>

> All arguments of the stored procedure will be validated and constructed

> my my application.

>

> My real question is why can I use OPENROWSET function in stored

> procedure when I build the query

>

> using varchar variable and then execute it, although I can't do it

> direclty using OPENROWSET in

>

> stored procedure.

>

>

>

>

>

>|||

NNTP User,

Yes,I can not use directly OPENROWSET function in strored procedure, but I can use dynamic sql.

My problem is not how to store image using stored procedure (I can do it, however I don't like the way I do it)

but why can't I do it directly.

By the way I tried to use sp_executesql to run the code but it didn't work as well - so for now the only way

to store image or file using stored procedure is dynamic sql.

But I'm still wandering WHY? Is it a bug or something like that?

|||

As Steve indicated, you will have to specify the filename as a literal in the OPENROWSET call. Otherwise you will have to use dynamic SQL to form the entire statement and execute it. And with dynamic SQL you will have protect against SQL injection attacks. Optionally, you can do the following for a bulk import process:

1. Create temporary table to hold the user accounts

2. Use the new BulkCopy managed API to stream the user data from the client to server

3. Write SP to dump the rows from temporary table to the main table

For adding or modifying single values, you can just have a SP with image parameter and manipulate the data in the table. There is no need to use OPENROWSET which requires a file (creation of file by client, server having permissions to access file, etc) among other things and parameterization is not straight-forward.

|||

Umachandar Jayachandran - MS,

I'd like to know if there is other than OPENROWSET form of loading images or binary files into MSSQL,

I could missed it in documentation. It is sure that there will be no possibility to sql injection using dynamic sql

in my case.

Probably I should mentioned it before, the problem I described is not critical for me, becouse I found solution.

The solution may not be ideal (especialy for me) but it still works. Anyway probably I'll use C# and ADO.NET to

perform put and get image from database.

Maybe you know why the only way to use OPENROWSET in stored procedure is to use dynamic sql.

Thanks

Wednesday, March 7, 2012

MSSQL to Text File? how? thanks...

As we know, MySQL have function to output its data into Text File using "Select * into outfile 'C:/mytext.txt".

Does MSSQL has "Select into Outfile" function? If yes, what is the function??

Thanks in advance :)

Anderson

Do you have access to the SQL Manager GUI? You can output to a file from that.

You can also useSQLCMD -i Input.sql -o C:\mytext.txt -e from the command prompt.

|||P.S. - I'm not positive, but I don't think you can do it directly from the SQL statement like you can in MySQL.|||

You can use supplied BCP application or SQL Server Management Studio Express.

Example for BCP:

c:\Program Files\Microsoft SQL Server\90\Tools\Binn>bcp RZBWSPR.dbo.MenuItem out Result.txt -T -S DVESIC\SQLEXPRESS -c

|||You can call bcp utility with xp_cmdshell from T-SQL. Take a look at this post:
http://forums.asp.net/thread/1388531.aspx

Monday, February 20, 2012

MSSQL Select commands to FTP

Hello,

Im new to Sql Server Integration Services.

I need to get the data I get from my SQL Select statement into TXT, XML, CSV or Excel file format and then send it with FTP.

What is the easiest method that you recommend that I use?

Thank you very much!!

Data flow to select your records using an OLE DB source. Send those records to a text file (or csv) using the flat file destination.

Then in the control flow, use the FTP task to send that file.|||

Thank you very much for the reply.

I know this sounds simple, but do you know any tutorial out there that shows how to do this?

Thanks again in advance.

|||Try looking through the SSIS tutorials. What you need is pretty basic stuff.

http://msdn2.microsoft.com/en-us/library/ms167031.aspx|||

Thank you so much.

I have one complexity though.

My SSIS flow is gonna be run every day as a sceduled task.

The first time it runs, it has to send a History (Select Statement 1) but every day after that it just has to get the latest info (Select Statement 2).

How do I do this?

Thank you very much in advance!!

MSSQL Script execute in .bat

Hi Guys, anyone out there who could help me with my problem?

I have created a .bat file that would execute the mssql script and give an .txt file as an output. The script run successfully except for the output, the output save as .txt file was not in order unlike the output that was derrived from the query analyzer. As I try to solve my problem I observe that the lines separating the header and the data was the cause of un-order format, the lines doubles its length when I use the .bat file to execute my script. I have tried using the substring syntax to limit the length but same results achieved. Could anyone help me to remove those line automatically when I run my .bat file?

Please see the outputs below. Thank you very much.

output from query analyzer

object_name counter_name
-------- -----------
SQLServer:Buffer Manager Buffer cache hit ratio
SQLServer:Buffer Manager Buffer cache hit ratio base

(2 row(s) affected)

ouput from .bat

object_name
counter_name
----------------
----------------------
SQLServer:Buffer Manager
Buffer cache hit ratio
SQLServer:Buffer Manager
Buffer cache hit ratio baseFor anyone who will encounter same problem as I did, the solution for my problem was on the .bat file and not in MSSQL. I added the -W in the command in my .bat file.

MS-SQL one .NDF file missing How to Recover/Rebuild DB?

Dear All,
SOS Please Help.
I have a MS-SQL DB with 4 .ndf files. One (first) .ndf file is missing.
somehow got deleted??. Is there any way can rebuild my DB.

The .MDF and .LDF files are in tact.
Please help asap.

Dhumbak

*** Sent via Developersdex http://www.developersdex.com ***It depends what this NDF was created for in the first place. If it was
storing indexes then you can rebuild these indexes. If it was data and
you have no backup on the filegroup then you probably lost your data in
loosing the NDF.

Dhungu Dhumbak wrote:

Quote:

Originally Posted by

Dear All,
SOS Please Help.
I have a MS-SQL DB with 4 .ndf files. One (first) .ndf file is missing.
somehow got deleted??. Is there any way can rebuild my DB.
>
The .MDF and .LDF files are in tact.
Please help asap.
>
Dhumbak
>
*** Sent via Developersdex http://www.developersdex.com ***