2016年6月26日 星期日

How can I declare a Boolean parameter or column in stored procedure/SQL statement/DB table in SQL Server?

Most of the time, our program would have some Boolean type variables would like to be stored in a database. However, not all of the popular Database support Boolean data type (e.g. SQL Server).

In order to handle Boolean data type, we can declare 'bit' data type for this purpose.

e.g.
DECLARE @MyVar bit
Set @MyVar = 'false' /* False */
Select @MyVar;   /* Result is 0 */


DECLARE @MyVar bit
Set @MyVar = 'true'  /* True */
Select @MyVar;   /* Result is 1 */


DECLARE @MyVar bit
Set @MyVar = 0   /* False */
Select @MyVar;   /* Result is 0 */


DECLARE @MyVar bit
Set @MyVar = 1   /* True */
Select @MyVar;   /* Result is 1 */


Remark:
For testing the result, apart from using "Select @MyVar", useing "print @MyVar" could also achieve the same purpose. For example:

DECLARE @MyVar bit
Set @MyVar = 'false'
print @MyVar; /* Result is 0 */



沒有留言:

張貼留言