在SQL Server中使用单引号作为存储过程参数

在SQL Server中使用单引号作为存储过程参数

简介

首先,让我们了解一下最受欢迎的SQL软件,它被称为SQL(结构化查询语言)Server。这个服务器是由最受欢迎的公司引入和开发的,公司的名字是微软。

这是一个关系数据库服务器。该产品旨在执行其他应用程序需要的数据存储和检索的基本任务。它可以在单个计算机或由网络连接的多台计算机上运行。

这个SQL Server于1989年4月24日发布。它可以轻松下载。SQL Server也是一款开源软件。

现在,让我们了解一下在SQL Server中用于存储过程的单引号。

字符串表示只能使用单引号(”)。

如果字符串没有使用单引号,或者可能使用双引号,编译器会抛出一个运行时错误,让我们知道我们犯了一个错误。

我执行SQL操作的服务器是SQL SERVER 2019。

存储过程

一个可以保存和重复使用的SQL编码称为存储过程。

因此,如果您经常编写SQL查询,请将它们保存为存储过程并仅调用它们来运行。

此外,您可以将参数发送到存储过程,让它根据过去参数的值采取相应的操作。

语法

CREATE PROCEDURE procedure name
AS
SQL Statement
GO;

示例程序

在学习示例程序之前,让我们先了解这些过程在SQL Server上是如何执行的。

语法

DECLARE @variable name Data Type
@varaiable name = Variable Value
SELECT @variable name AS Result
GO

这是该过程的语法。现在让我们立即进入示例

示例1

代码

1> DECLARE @var1 VARCHAR(100)
2> SET @var1 = 'Believe Become'
3> SELECT @var1 AS Result
4> go

输出结果

Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Believe Become

(1 rows affected)

例子 2

代码

1> DECLARE @var2 INT
2> SET @var2 = 269
3> SELECT @var2 AS Result
4> go

输出

Result
_ _ _ _ _
        269

(1 rows affected)

示例2

代码

1> DECLARE @var2 INT
2> SET @var2 = 269
3> SELECT @var2 AS Result
4> go

输出

Result
_ _ _ _ _
        269

(1 rows affected)

示例3

代码

1> DECLARE @var3 FLOAT
2> SET @var3 = 829.17895
3>  SELECT @var3 AS Result
4> go

输出

Result
_ _ _ _ _ _ _ _ _ _ 
      829.17894999999999
(1 rows affected)

示例4

代码

1> DECLARE @var4 Character
2> SET @var4 = 'c'
3>  SELECT @var4 AS Result
4> go

输出

Result
_ _ _
c

(1 rows affected)

示例5

代码

1> DECLARE @var5 VARCHAR (100)
2> SET @var5 = 'I am lost for words. My mouth is probably dry out of emotions'
3> SELECT @var5 AS Result
4> go

输出

Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
I am lost for words. My mouth is probably dry out of emotions
(1 rows affected) 

示例6

代码

1> DECLARE @var6 VARCHAR (100)
2> SET @var6 = I am lost for words. My mouth is probably dry out of emotions.
3> SELECT @var6 AS Result
4> go

输出

Result
Msg 102, Level 15, State 1, Server DESKTOP-Q5M2SGA, Line 2
Incorrect syntax near 'am'.

示例7

代码

1> DECLARE @var5 VARCHAR (100)
2> SET @var5 = Do not be afraid to give up good to do great
3> SELECT @var5 AS Result
4> go

输出

Result
Msg 156, Level 15, State 1, Server DESKTOP-Q5M2SGA, Line 2
Incorrect syntax near the keyword 'not'.

这就是关于单引号案例研究的全部内容。现在让我们进入Procedure Parameter time。但在跳入之前,让我们了解一下什么是procedure。

存储过程

它类似于其他编程语言中的函数或方法。在每个procedure中都有一些参数。在调用procedure时,这些参数会传入一些值。参数的数据类型在procedure本身中已经指定。

即使对于存储过程,输入也必须只有单引号。

我们在MS SQL Server中执行此操作。它也被称为SQL Server。

存储过程1

1> CREATE PROCEDURE THINK (@String VARCHAR(100))
2>
3> AS
4>
5> SELECT @String AS Result
6> go

使用现在的过程

代码1

1> EXEC THINK 'THINK'
2> go

输出

Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
THINK

(1 rows affected)

代码2

1> EXEC THINK 'a b c d e f g h I j k l m n o p q r s t u v w x y z'
2> go

输出

Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
a b c d e f g h I j k l m n o p q r s t u v w x y z
(1 rows affected)

代码 3

1> EXEC THINK 'I had goosebumps, one of the greats of this game talking about Zimbabwe and in particular me'
2> EXEC THINK 'He just seems to know exactly what to do, when to do it.'
3> go

输出

Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
I had goosebumps, one of the greats of this game talking about Zimbabwe and in particular me

(1 rows affected)
Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
He just seems to know exactly what to do, when to do it.

(1 rows affected)

代码 4

1> EXEC THINK He just seems to know exactly what to do, when to do it.
2> goe

输出

Msg 102, Level 15, State 1, Server DESKTOP-Q5M2SGA, Line 1
Incorrect syntax near 'just'.

代码 5

1> EXEC THINK 'This product is designed to perform the fundamental task of data storage and retrieval as needed by other applications'
2> go

输出

Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
This product is designed to perform the fundamental task of data storage and retrieval as needed by other applications
(1 rows affected)

代码6

1> EXEC THINK ' Online Python DebBugger.  Code, Run and Debug Python program online. Please Compile Your Codes '
2> go

输出

Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 Online Python DebBugger.  Code, Run and Debug Python program online. Please Compile Your Codes 
 (1 rows affected)

代码7

1> EXEC THINK ' Like Raza, he was also full of praise for the fast bowlers for setting the tone early '
2> go

输出

Result
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 Like Raza, he was also full of praise for the fast bowlers for setting the tone early 
(1 rows affected)

这一切都关于在SQL Server中使用单引号作为存储过程参数的话题。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程