Showing posts with label binary. Show all posts
Showing posts with label binary. Show all posts

Sunday, February 19, 2012

DocumentID not returned in Adventureworks

The Large Binary Object example installed with SQL Server 2005 has a table with the DocumentID as the key. When the row is inserted it should return the DocumentID that was created. When you debug the example stored procedure you can see the documentID is created and correct. When you run the sample unmodified it always returns -1 as the documentID. It appears the stored procedure is catching an exception and assigning the documentID a value of -1 as seen here in the Adventureworks LOB sample sored procedure (below).

However, it appears that something with the datbase engine or proce is broken and Microsoft knew this becasue the assignment to documentID upon return from the stored proc is commented out (see below). In fact the whole sample is faked because when you follow it you realize that it is reading into the database one document and writing out one that already existed in the database..

Does anyone know how to get the DocumentID back from the stored procedure that inserted the binary object into the Documents table of Adventureworks?

Thanks in advance.

Portion of Adventure works usp_InsertDocument

RAISERROR ('Insert failed.', 16, 1);

END TRY

BEGIN CATCH

SET @.DocumentID = -1;

RETURN(1);

END CATCH;

END -- END of usp_InsertDocument

;

Portion of LargeBinaryObject.cs

using (SqlConnection sqlConn = new SqlConnection(connectionString))

{

using (SqlCommand sprocCommand = new SqlCommand("[Production].[usp_InsertDocument]", sqlConn))

{

sqlConn.Open();

sprocCommand.CommandType = CommandType.StoredProcedure;

// Add time to the title because there is an unique constraint on this column.

sprocCommand.Parameters.Add(new SqlParameter("@.Title", SqlDbType.NVarChar, 50));

sprocCommand.Parameters[0].Value = fileName + DateTime.Now.TimeOfDay.ToString();

sprocCommand.Parameters.Add(new SqlParameter("@.FileName", SqlDbType.NVarChar, 400));

sprocCommand.Parameters[1].Value = fullFileName;

sprocCommand.Parameters.Add(new SqlParameter("@.FileExtension", SqlDbType.NVarChar, 8));

sprocCommand.Parameters[2].Value = fileExtension;

sprocCommand.Parameters.Add(new SqlParameter("@.Status", SqlDbType.TinyInt));

sprocCommand.Parameters[3].Value = 1;

sprocCommand.Parameters.Add(new SqlParameter("@.Document", SqlDbType.Image));

sprocCommand.Parameters[4].Value = bytes;

sprocCommand.Parameters.Add(new SqlParameter("@.DocumentID", SqlDbType.Int));

sprocCommand.Parameters[5].Direction = ParameterDirection.Output;

sprocCommand.ExecuteNonQuery();

//int DocumentID = (int)sprocCommand.Parameters[5].Value;

}

This here is a pretty easy one:

SqlCommand cmd = new SqlCommand("usp_YourProc");

cmd.CommandType = CommandType.StoredProcedure;

cmd.Connection = conn;

SqlParameter param_InputoutputTest = new SqlParameter("@.InputoutputTest", SqlDbType.Int);

param_InputoutputTest.Value = "1";

param_InputoutputTest.Direction = ParameterDirection.InputOutput;

SqlParameter param_ReturntTest = new SqlParameter("@.ReturntTest", SqlDbType.Int);

param_ReturntTest.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(param_InputoutputTest);

cmd.Parameters.Add(param_ReturntTest);

conn.Open();

cmd.ExecuteNonQuery();

Console.WriteLine(cmd.Parameters["@.ReturntTest"].Value.ToString());

Console.WriteLine(cmd.Parameters["@.InputoutputTest"].Value.ToString());

conn.Close();

ALTER PROCEDURE usp_YourProc

(

@.InputoutputTest INT OUTPUT

)

AS

SELECT @.InputoutputTest = -1

RETURN 2

HTH, Jens K. Suessmeyer.

-
http://www.sqlserver2005.de
-

Tuesday, February 14, 2012

DOCI to Binary

Is possible to change an existing database from DOCI to Binary?
Thanks,
Dean
Dean,
Are you referring to changing collation of the database here? If you change
the collation of a database using ALTER DATABASE only new columns will use
the new collation. Existing columns will use their old collation. To change
existing columns from one collation to a new one, you must use ALTER
TABLE...ALTER COLUMN for all tables. This is a lot of work and you may run
into issues with various constraints that you may have defined.
You may find it easier to create a new database with the new collation and
then DTS the data over from the old database. You can then drop the old
database, and rename your new database to the old name. I hope I read your
vague question correctly, and I'm not barking up the wrong tree... :-)
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
"Dean" <anonymous@.discussions.microsoft.com> wrote in message
news:EBE6DFAC-0886-4646-A296-C83762CEB875@.microsoft.com...
> Is possible to change an existing database from DOCI to Binary?
> Thanks,
> Dean
>
|||You read it correctly and thanks for the answer.

DOCI to Binary

Is possible to change an existing database from DOCI to Binary
Thanks
DeaDean,
Are you referring to changing collation of the database here? If you change
the collation of a database using ALTER DATABASE only new columns will use
the new collation. Existing columns will use their old collation. To change
existing columns from one collation to a new one, you must use ALTER
TABLE...ALTER COLUMN for all tables. This is a lot of work and you may run
into issues with various constraints that you may have defined.
You may find it easier to create a new database with the new collation and
then DTS the data over from the old database. You can then drop the old
database, and rename your new database to the old name. I hope I read your
vague question correctly, and I'm not barking up the wrong tree... :-)
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
"Dean" <anonymous@.discussions.microsoft.com> wrote in message
news:EBE6DFAC-0886-4646-A296-C83762CEB875@.microsoft.com...
> Is possible to change an existing database from DOCI to Binary?
> Thanks,
> Dean
>

DOCI to Binary

Is possible to change an existing database from DOCI to Binary?
Thanks,
DeanDean,
Are you referring to changing collation of the database here? If you change
the collation of a database using ALTER DATABASE only new columns will use
the new collation. Existing columns will use their old collation. To change
existing columns from one collation to a new one, you must use ALTER
TABLE...ALTER COLUMN for all tables. This is a lot of work and you may run
into issues with various constraints that you may have defined.
You may find it easier to create a new database with the new collation and
then DTS the data over from the old database. You can then drop the old
database, and rename your new database to the old name. I hope I read your
vague question correctly, and I'm not barking up the wrong tree... :-)
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
"Dean" <anonymous@.discussions.microsoft.com> wrote in message
news:EBE6DFAC-0886-4646-A296-C83762CEB875@.microsoft.com...
> Is possible to change an existing database from DOCI to Binary?
> Thanks,
> Dean
>|||You read it correctly and thanks for the answer.