Showing posts with label type. Show all posts
Showing posts with label type. Show all posts

Wednesday, March 28, 2012

Read, write and update xml data type.

Hi All,

I would like to learn about xml data type of sql server 2005. I am using c# to develop a project that will use sql server express as a database. What I want to accomplish in my project is to serialize an object and save into a field with xml data type. Also I want to have same functionality in other way around. Retrieve this xml representation of the object, Deserialize it so get the saved object back into application.

I would be happy If you can provide me some code which shows how to accomplish this task or some links that directs me to the appropriate docs.

Thanks in advance.

There is lots of material on MSDN, there is a section about the xml data type with subsections about the methods of the xml data type (i.e. query, value, exist, modify, nodes) and the XML DML (XML data modification language).|||

Hi Martin,

Thanks for the reply and links. I haven't gone through the links you sent completly but I think the subject I typed here is misleading so let me explain a bit more, the difficulty I have. . I have no problem with creating a table with an xml type field. I believe by reading the links you have sent I can perform insert, update and delete functions. My difficulty starts just after the seriliazation of an object or just before the deserialization of the xml data I read from xml field. Both serialize and deserialize methods trys to write/read to/from a file or a stream. All I want is to hold this xml in a data structure where I can use it at the time of serialization or deserialization.

The original situaltion:

I have a form to be filled out by the users of my application. Since there are so many fields of data in this form I dont want to create a table with somany fields. So I am planing to hold info entered by user in an xml field. (and ofcourse I should be able to read the data back from this xml field and display in the app)

Thanks.

|||

Got it worked Smile

I used string/TextWriter and string/TextReader combinations and worked fine. Thanks.

FlatWhite

|||

Hi FlatWhite

I would like to do the same thing you are doing. Could you provide some code snippets for me on how to do it?

Thanks

|||

Well I am neither SQL nor C# expert so you can use my code at your own risk J

First of all I am assuming that you have an SQL database table which has ID and XML fields and also you have InsertObj and SelectObj stored procedures.

TABLE : ObjectTable

ObjID-int-identity

ObjXML-XML

SPs : InsertObj

SelectObj

Code Snippet

CREATE PROCEDURE InsertObj

@.ObjXML xml

AS

BEGIN

INSERT INTO ObjectTable (ObjXML)

VALUES (@.ObjXML)

END

CREATE PROCEDURE SelectObj

@.ObjID int

AS

BEGIN

SELECT ObjXML

FROM ObjTable

WHERE ObjID = @.ObjID

END

I am also assuming that you have class Ojb with three properties property1, property2 and property3 and a form with 4 textboxes

Here is how you can serialize an object and save it in an XML field. (There might be some better way of doing this but sofar no one made a comment on this)

Code Snippet

Obj c = new Obj();

c.property1 = textBox1.Text;

c.property2 = textBox2.Text;

c.property3 = textBox3.Text;

XmlSerializer s = new XmlSerializer(typeof(Obj));

System.Text.StringBuilder builder = new System.Text.StringBuilder();

s.Serialize(XmlWriter.Create(builder),c);

SqlConnection conn = new SqlConnection();

conn.ConnectionString = @."Data Source=Server;Initial Catalog=database;Integrated Security=SSPI;";

conn.Open();

SqlCommand command = conn.CreateCommand();

command.CommandText = "InsertObj";

command.CommandType = System.Data.CommandType.StoredProcedure;

command.Parameters.Add("@.ObjXML", System.Data.SqlDbType.Xml);

command.Parameters[0].Value = builder.ToString();

command.ExecuteNonQuery();

conn.Close();

And this is how you can deserialize an XML field and get back the saved object.

Code Snippet

XmlReaderSettings set = new XmlReaderSettings();

set.ConformanceLevel = ConformanceLevel.Fragment;

Obj c = new Obj();

SqlConnection conn = new SqlConnection();

conn.ConnectionString = @."Data Source=server;Initial Catalog=database;Integrated Security=SSPI;";

conn.Open();

SqlCommand command = conn.CreateCommand();

command.CommandText = "SelectObj";

command.CommandType = System.Data.CommandType.StoredProcedure;

command.Parameters.Add("@.ObjID", System.Data.SqlDbType.Int);

//taking the input from textBox4.

command.Parameters[0].Value = Convert.ToInt32(textBox4.Text);

SqlDataReader datareader = command.ExecuteReader();

System.Text.StringBuilder builder = new System.Text.StringBuilder();

XmlSerializer s = new XmlSerializer(typeof(Obj));

while(datareader.Read())

{

builder.Append(datareader[0]);

}

TextReader tr = new StringReader(builder.ToString());

c = (CObj)s.Deserialize(tr);

tr.Close();

textBox1.Text = c.property1;

textBox2.Text = c.property2;

textBox3.Text = c.property3;

I hope it helps.

Read, write and update xml data type.

Hi All,

I would like to learn about xml data type of sql server 2005. I am using c# to develop a project that will use sql server express as a database. What I want to accomplish in my project is to serialize an object and save into a field with xml data type. Also I want to have same functionality in other way around. Retrieve this xml representation of the object, Deserialize it so get the saved object back into application.

I would be happy If you can provide me some code which shows how to accomplish this task or some links that directs me to the appropriate docs.

Thanks in advance.

There is lots of material on MSDN, there is a section about the xml data type with subsections about the methods of the xml data type (i.e. query, value, exist, modify, nodes) and the XML DML (XML data modification language).|||

Hi Martin,

Thanks for the reply and links. I haven't gone through the links you sent completly but I think the subject I typed here is misleading so let me explain a bit more, the difficulty I have. . I have no problem with creating a table with an xml type field. I believe by reading the links you have sent I can perform insert, update and delete functions. My difficulty starts just after the seriliazation of an object or just before the deserialization of the xml data I read from xml field. Both serialize and deserialize methods trys to write/read to/from a file or a stream. All I want is to hold this xml in a data structure where I can use it at the time of serialization or deserialization.

The original situaltion:

I have a form to be filled out by the users of my application. Since there are so many fields of data in this form I dont want to create a table with somany fields. So I am planing to hold info entered by user in an xml field. (and ofcourse I should be able to read the data back from this xml field and display in the app)

Thanks.

|||

Got it worked Smile

I used string/TextWriter and string/TextReader combinations and worked fine. Thanks.

FlatWhite

|||

Hi FlatWhite

I would like to do the same thing you are doing. Could you provide some code snippets for me on how to do it?

Thanks

|||

Well I am neither SQL nor C# expert so you can use my code at your own risk J

First of all I am assuming that you have an SQL database table which has ID and XML fields and also you have InsertObj and SelectObj stored procedures.

TABLE : ObjectTable

ObjID-int-identity

ObjXML-XML

SPs : InsertObj

SelectObj

Code Snippet

CREATE PROCEDURE InsertObj

@.ObjXML xml

AS

BEGIN

INSERT INTO ObjectTable (ObjXML)

VALUES (@.ObjXML)

END

CREATE PROCEDURE SelectObj

@.ObjID int

AS

BEGIN

SELECT ObjXML

FROM ObjTable

WHERE ObjID = @.ObjID

END

I am also assuming that you have class Ojb with three properties property1, property2 and property3 and a form with 4 textboxes

Here is how you can serialize an object and save it in an XML field. (There might be some better way of doing this but sofar no one made a comment on this)

Code Snippet

Obj c = new Obj();

c.property1 = textBox1.Text;

c.property2 = textBox2.Text;

c.property3 = textBox3.Text;

XmlSerializer s = new XmlSerializer(typeof(Obj));

System.Text.StringBuilder builder = new System.Text.StringBuilder();

s.Serialize(XmlWriter.Create(builder),c);

SqlConnection conn = new SqlConnection();

conn.ConnectionString = @."Data Source=Server;Initial Catalog=database;Integrated Security=SSPI;";

conn.Open();

SqlCommand command = conn.CreateCommand();

command.CommandText = "InsertObj";

command.CommandType = System.Data.CommandType.StoredProcedure;

command.Parameters.Add("@.ObjXML", System.Data.SqlDbType.Xml);

command.Parameters[0].Value = builder.ToString();

command.ExecuteNonQuery();

conn.Close();

And this is how you can deserialize an XML field and get back the saved object.

Code Snippet

XmlReaderSettings set = new XmlReaderSettings();

set.ConformanceLevel = ConformanceLevel.Fragment;

Obj c = new Obj();

SqlConnection conn = new SqlConnection();

conn.ConnectionString = @."Data Source=server;Initial Catalog=database;Integrated Security=SSPI;";

conn.Open();

SqlCommand command = conn.CreateCommand();

command.CommandText = "SelectObj";

command.CommandType = System.Data.CommandType.StoredProcedure;

command.Parameters.Add("@.ObjID", System.Data.SqlDbType.Int);

//taking the input from textBox4.

command.Parameters[0].Value = Convert.ToInt32(textBox4.Text);

SqlDataReader datareader = command.ExecuteReader();

System.Text.StringBuilder builder = new System.Text.StringBuilder();

XmlSerializer s = new XmlSerializer(typeof(Obj));

while(datareader.Read())

{

builder.Append(datareader[0]);

}

TextReader tr = new StringReader(builder.ToString());

c = (CObj)s.Deserialize(tr);

tr.Close();

textBox1.Text = c.property1;

textBox2.Text = c.property2;

textBox3.Text = c.property3;

I hope it helps.

Read xml from sql server column(xml data type) and return as XmlDocument in C#

Hi Everyone:

I would appreciate it if someone can help me this problem as I am a c# beginner. I am writing a C# method that is supposed to read in XML from SQL Server 2005 Database. The table I am reading from has a column which is set to XML data type and has a well formed XML document. I would like to load that xml into a xml document in my c# and return back as a xmldocument in my method. Here is my method so far, as you can see i am utilizing the Enterprise Library Data Access Block for db access. I woud appreciate if you can provide me with some help. Thanks.

Note: What i am doing right now maybe completely off track from what I am trying to achieve. here is the code I have so far:

private const string SQLGETCNFS = "SELECT EntityDefinitionXML FROM EntityDefinition WHERE EntityID = ";

/// <summary>
/// Method retrieves XML Entity Definition from the configuration database
/// </summary>
/// <param name="entityID"></param>
/// <returns>XmlDocument Type</returns>
public XmlDocument GetCustomerCNSFData(string entityID)
{
//connect to config database
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = SQLGETCNFS + entityID.ToString();

IDataReader reader = db.ExecuteReader(CommandType.Text, sqlCommand);


while (reader.Read())
{
SqlXml sx = reader.GetSqlXml(1);
XmlReader xr = sx.CreateReader();
xr.Read();
}
return null;
}

You can create a new instance of XmlDocument and then call the Load method and pass the XmlReader that you created from SqlXml.

Regards,

Galex Yen

read varbinary(max)

Hi,
I'm working with visual c++ and usign MFC ( ODBC ) . I have a problem
with varbinary(max) type of sql server 2005 express. I can read a
varbinary (n ) but when i try to read a varbinary(max) fails.
CDBVariant cvarValor;
m_tablaDeConsulta.GetFieldValue ( campo,
cvarValor );
I use this code to read varbinary fields. The only differrence betwen
max a normal varbinary is the precision. When i read a varbinary
precision is 0.
Someone can help me?
thanks.AKS ha escrito:

> Hi,
> I'm working with visual c++ and usign MFC ( ODBC ) . I have a problem
> with varbinary(max) type of sql server 2005 express. I can read a
> varbinary (n ) but when i try to read a varbinary(max) fails.
> CDBVariant cvarValor;
> m_tablaDeConsulta.GetFieldValue ( campo,
> cvarValor );
> I use this code to read varbinary fields. The only differrence betwen
> max a normal varbinary is the precision. When i read a varbinary
> precision is 0.
> Someone can help me?
> thanks.
At least i can read a varbinary. The code
// get data size
void* buffer = malloc ( 1 );
SQLINTEGER tam = 0;
tam = m_tablaDeConsulta.GetData ( m_tablaDeConsulta.m_pDatabase,
m_tablaDeConsulta.m_hstmt, ind+1, SQL_C_BINARY, buffer, 0, SQL_C_BINARY
);
free (buffer );
// alloc data buffer
CDBVariant cvarValor;
buffer = m_tablaDeConsulta.GetDataBuffer( cvarValor, SQL_C_BINARY,
&tam, SQL_C_BINARY, tam);
// read data
m_tablaDeConsulta.GetData ( m_tablaDeConsulta.m_pDatabase,
m_tablaDeConsulta.m_hstmt, ind+1, SQL_C_BINARY, buffer, tam,
SQL_C_BINARY );

Monday, March 26, 2012

read only cell

I try to insert a value to a column and it gives me, "the cell is read
only" and won't allow me to type in the value. I am using MS SQL 2005. Can
anyone please tell me how to turn off the read only property? Thanks.
I figured out that the column identity value is to true. How do I turn it to
false? It won't allow me to do it in SQL Server Management Studio. Thanks.
" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:exl6Eg3mHHA.4552@.TK2MSFTNGP05.phx.gbl...
>I try to insert a value to a column and it gives me, "the cell is read
>only" and won't allow me to type in the value. I am using MS SQL 2005. Can
>anyone please tell me how to turn off the read only property? Thanks.
>
|||No sweat. I got it. Sorry.
" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:uaMf2m3mHHA.4772@.TK2MSFTNGP05.phx.gbl...
>I figured out that the column identity value is to true. How do I turn it
>to false? It won't allow me to do it in SQL Server Management Studio.
>Thanks.
>
> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
> news:exl6Eg3mHHA.4552@.TK2MSFTNGP05.phx.gbl...
>
|||" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:%23zgzYx3mHHA.596@.TK2MSFTNGP06.phx.gbl...
> No sweat. I got it. Sorry.
>
Hmm, I'm a little concerned.
Generally editing data using SMS isn't that great of an idea.
And turning off an Identity column is also generally a bad idea.
You may want to think about how you're doing whatever you're doing.

> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
> news:uaMf2m3mHHA.4772@.TK2MSFTNGP05.phx.gbl...
>
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html
|||Thanks for your concern. The identity field is wrong in the first place. So
don't worry.
"Greg D. Moore (Strider)" <mooregr_deleteth1s@.greenms.com> wrote in message
news:%23F%23MEu5mHHA.1388@.TK2MSFTNGP05.phx.gbl...
>" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
>news:%23zgzYx3mHHA.596@.TK2MSFTNGP06.phx.gbl...
> Hmm, I'm a little concerned.
> Generally editing data using SMS isn't that great of an idea.
> And turning off an Identity column is also generally a bad idea.
> You may want to think about how you're doing whatever you're doing.
>
>
> --
> Greg Moore
> SQL Server DBA Consulting Remote and Onsite available!
> Email: sql (at) greenms.com
> http://www.greenms.com/sqlserver.html
>

read only cell

I try to insert a value to a column and it gives me, "the cell is read
only" and won't allow me to type in the value. I am using MS SQL 2005. Can
anyone please tell me how to turn off the read only property? Thanks.I figured out that the column identity value is to true. How do I turn it to
false? It won't allow me to do it in SQL Server Management Studio. Thanks.
" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:exl6Eg3mHHA.4552@.TK2MSFTNGP05.phx.gbl...
>I try to insert a value to a column and it gives me, "the cell is read
>only" and won't allow me to type in the value. I am using MS SQL 2005. Can
>anyone please tell me how to turn off the read only property? Thanks.
>|||No sweat. I got it. Sorry.
" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:uaMf2m3mHHA.4772@.TK2MSFTNGP05.phx.gbl...
>I figured out that the column identity value is to true. How do I turn it
>to false? It won't allow me to do it in SQL Server Management Studio.
>Thanks.
>
> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
> news:exl6Eg3mHHA.4552@.TK2MSFTNGP05.phx.gbl...
>|||" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:%23zgzYx3mHHA.596@.TK2MSFTNGP06.phx.gbl...
> No sweat. I got it. Sorry.
>
Hmm, I'm a little concerned.
Generally editing data using SMS isn't that great of an idea.
And turning off an Identity column is also generally a bad idea.
You may want to think about how you're doing whatever you're doing.

> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
> news:uaMf2m3mHHA.4772@.TK2MSFTNGP05.phx.gbl...
>
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html|||Thanks for your concern. The identity field is wrong in the first place. So
don't worry.
"Greg D. Moore (Strider)" <mooregr_deleteth1s@.greenms.com> wrote in message
news:%23F%23MEu5mHHA.1388@.TK2MSFTNGP05.phx.gbl...
>" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
>news:%23zgzYx3mHHA.596@.TK2MSFTNGP06.phx.gbl...
> Hmm, I'm a little concerned.
> Generally editing data using SMS isn't that great of an idea.
> And turning off an Identity column is also generally a bad idea.
> You may want to think about how you're doing whatever you're doing.
>
>
> --
> Greg Moore
> SQL Server DBA Consulting Remote and Onsite available!
> Email: sql (at) greenms.com
> http://www.greenms.com/sqlserver.html
>

read only cell

I try to insert a value to a column and it gives me, "the cell is read
only" and won't allow me to type in the value. I am using MS SQL 2005. Can
anyone please tell me how to turn off the read only property? Thanks.I figured out that the column identity value is to true. How do I turn it to
false? It won't allow me to do it in SQL Server Management Studio. Thanks.
" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:exl6Eg3mHHA.4552@.TK2MSFTNGP05.phx.gbl...
>I try to insert a value to a column and it gives me, "the cell is read
>only" and won't allow me to type in the value. I am using MS SQL 2005. Can
>anyone please tell me how to turn off the read only property? Thanks.
>|||No sweat. I got it. Sorry.
" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:uaMf2m3mHHA.4772@.TK2MSFTNGP05.phx.gbl...
>I figured out that the column identity value is to true. How do I turn it
>to false? It won't allow me to do it in SQL Server Management Studio.
>Thanks.
>
> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
> news:exl6Eg3mHHA.4552@.TK2MSFTNGP05.phx.gbl...
>>I try to insert a value to a column and it gives me, "the cell is read
>>only" and won't allow me to type in the value. I am using MS SQL 2005. Can
>>anyone please tell me how to turn off the read only property? Thanks.
>|||" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
news:%23zgzYx3mHHA.596@.TK2MSFTNGP06.phx.gbl...
> No sweat. I got it. Sorry.
>
Hmm, I'm a little concerned.
Generally editing data using SMS isn't that great of an idea.
And turning off an Identity column is also generally a bad idea.
You may want to think about how you're doing whatever you're doing.
> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
> news:uaMf2m3mHHA.4772@.TK2MSFTNGP05.phx.gbl...
>>I figured out that the column identity value is to true. How do I turn it
>>to false? It won't allow me to do it in SQL Server Management Studio.
>>Thanks.
>>
>> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
>> news:exl6Eg3mHHA.4552@.TK2MSFTNGP05.phx.gbl...
>>I try to insert a value to a column and it gives me, "the cell is read
>>only" and won't allow me to type in the value. I am using MS SQL 2005.
>>Can anyone please tell me how to turn off the read only property? Thanks.
>>
>
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html|||Thanks for your concern. The identity field is wrong in the first place. So
don't worry.
"Greg D. Moore (Strider)" <mooregr_deleteth1s@.greenms.com> wrote in message
news:%23F%23MEu5mHHA.1388@.TK2MSFTNGP05.phx.gbl...
>" 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
>news:%23zgzYx3mHHA.596@.TK2MSFTNGP06.phx.gbl...
>> No sweat. I got it. Sorry.
> Hmm, I'm a little concerned.
> Generally editing data using SMS isn't that great of an idea.
> And turning off an Identity column is also generally a bad idea.
> You may want to think about how you're doing whatever you're doing.
>
>> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
>> news:uaMf2m3mHHA.4772@.TK2MSFTNGP05.phx.gbl...
>>I figured out that the column identity value is to true. How do I turn it
>>to false? It won't allow me to do it in SQL Server Management Studio.
>>Thanks.
>>
>> " 00ScarlettJohnson" <EE@.yahoo.com> wrote in message
>> news:exl6Eg3mHHA.4552@.TK2MSFTNGP05.phx.gbl...
>>I try to insert a value to a column and it gives me, "the cell is read
>>only" and won't allow me to type in the value. I am using MS SQL 2005.
>>Can anyone please tell me how to turn off the read only property?
>>Thanks.
>>
>>
>
> --
> Greg Moore
> SQL Server DBA Consulting Remote and Onsite available!
> Email: sql (at) greenms.com
> http://www.greenms.com/sqlserver.html
>sql

Tuesday, March 20, 2012

Re: Stored procedure not executing correctly

Hi everyone,

I am having trouble with this stored procedure. The @.curr_type stores the first value of inspection type of the recordset , @.inspec_type stores the current record of the inspection type. @.inspec_type moves to the next record while @.curr_type's value remain the same for comparisons.

The error is in the Else statment. These variables,@.curr_type and @.inspec_type, values are = 3 the else statement should not be executed.

Is something wrong with my code or logic?
Thank you for any assistance.Mind posting some code ?|||Yeah, without seeing it you want us to guess? Well, I guess that the ELSE is not executed because a check for a value of the variable does not take into account a possibility of NULL. Good enough, hey?! ;)|||so Sorry! absent minded me.

Here is my code:

CREATE PROCEDURE dbo.spCheck_inspection_type
@.inspec_type smallint Out, @.curr_type smallint Out
AS--ve

declare insp_type cursor

For
Select inspection_type
From tblTmp_inspec
Where partial <> 0 or complete <> 0;
set @.curr_type = 0;

Open insp_type
Fetch Next From insp_type Into @.inspec_type

While @.@.FETCH_STATUS = 0
Begin
if @.curr_type = 0
set @.curr_type = @.inspec_type;
Else
--last inspection type is @.curr_type
Begin
if @.curr_type <> @.inspec_type
close insp_type
deallocate insp_type
return(1);
End

Fetch Next From insp_type Into @.inspec_type

End

close insp_type
deallocate insp_type

return(0);
GO

The ELSE statment executed eventhough both variables 's values is 3 . Why?

Thank you much!!|||Indeting is SO overlooked...

You have logic problems...I guess they don't call them scop termintors here...

BUT...for now, to make it easier to read, use a BEGIN and END for every logic block...AND make sure to match them up...

BUT...This is WAY to over done for what you're trying to do...which is?

Here, look at this

CREATE PROC spCheck_inspection_type
@.inspec_type smallint OUT, @.curr_type smallint OuT
AS
DECLARE insp_type CURSOR
FOR
SELECT inspection_type FROM tblTmp_inspec WHERE partial <> 0 or complete <> 0;
SET @.curr_type = 0;

Open insp_type
FETCH NEXT FROM insp_type INTO @.inspec_type

WHILE @.@.FETCH_STATUS = 0
BEGIN
IF @.curr_type = 0
SET @.curr_type = @.inspec_type;
ELSE
BEGIN
IF @.curr_type <> @.inspec_type
CLOSE insp_type
DEALLOCATE insp_type
RETURN 1
END
FETCH NEXT FROM insp_type INTO @.inspec_type
END

CLOSE insp_type
DEALLOCATE insp_type

RETURN 0
GO|||That's too many colors,Brett, andno comment on what they mean!

All the guy is missing is BEGIN...END on the test for @.curr_type <> @.inspec_type: if @.curr_type <> @.inspec_type begin
close insp_type
deallocate insp_type
return(1);
end|||fine...fine...fine...

BUT!

Would you do this?

I mean, what does it even mean?|||Yup, there are problems with the code. Most obvious one is that the cursor declaration does not take into account the order of records as they get returned. This would yield unpredictable results when the app is running on a single CPU machine while being tested, compared to a prod environment when it gets deployed onto a SMP system.

To avoid usage of cursor, while fixing the ORDERing issue, would be to do the following:

declare @.curr_type int, @.record_id int
select @.curr_type = inspection_type, @.record_id = <record_id> from (
select top 1 <record_id>, inspection_type from tblTmp_inspec
WHERE partial <> 0 or complete <> 0
order by <record_id>) x

if exists (select 1 from tblTmp_inspec where <record_id> > @.record_id
and partial <> 0 or complete <> 0 and inspection_type <> @.curr_type)
return 1
else
return 0|||Brett, that was the most beautiful piece of SQL I've ever seen.

I am reminded of the Old Testament story of Joseph and his "Code of many colors". It must have looked something like that.

:)|||Brett, that was the most beautiful piece of SQL I've ever seen.
:)

And you know that's saying something...because he's, well, blind

Thanks Dude...

Seriously, alicejwz, Lettuce know what you're doing, and we can hook you up.

Re: Trouble in getting a value from bit data type in stored procedure

Hi eveyone,

I'm trying to get the stored procedure to return a value from a field in a table. The value in the field stores a bit value and default value is set to 0. So there should always a value in that field but it is giving me a null value. Can anyone see why.
I'm calling sp thru vb. Thanks much!

This is VB:
sub
Set cancel_inspection_query = Nothing
With cancel_inspection_query
.ActiveConnection = CurrentProject.Connection
.CommandText = "spInspec_cancel_initial_scan1"
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter("ret_val", adInteger, adParamReturnValue)
.Parameters.Append .CreateParameter("@.inspec_id", adInteger, adParamInput, 4, Me!inspecid.Caption)
.Parameters.Append .CreateParameter("@.bag_num", adInteger, adParamInput, 4, Me!bag_num.Caption)
.Parameters.Append .CreateParameter("@.sampling_id", adInteger, adParamInput, 4, Me!rmr.Caption)
.Execute , , adExecuteNoRecords

End With
Debug.Print cancel_inspection_query("ret_val").Value
end sub

This is sp:

CREATE PROCEDURE dbo.spInspec_cancel_initial_scan1
@.inspec_id int,
@.sampling_id int,
@.bag_num int
AS
declare @.inspection_complete bit

SELECT @.inspection_complete = inspection_complete
FROM dbo.tblBag_results
WHERE bag_num = @.bag_num;
begin
if @.inspection_complete= 1
return(1)
Else
if @.inspection_complete = 0
return(100)
--else
--return(-1)The Table DDL would help.

Is Bag_Num a PK or unique index?

If not, that's a problem...

Also is the column defined as NOT NULL?

If not, that's a problem...

And why not use an OUTPUT variable instead?

You should let SQL Server manage the return value. I've seen times when it overrides your value...which could be a problem if you code for a particular value...