SQL 函数、存储过程、游标与事务模板,学习sqlserver的朋友很多情况下都需要用得到。
1.标量函数:结果为一个单一的值,可包含逻辑处理过程。其中不能用getdate()之类的不确定性系统函数.代码如下:
--标量值函数
-- ================================================
-- Template generated from Template Explorer using:
-- Create Scalar Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION
(
-- Add the parameters for the function here
)
RETURNS
AS
BEGIN
-- Declare the return variable here
DECLARE
-- Add the T-SQL statements to compute the return value here
SELECT =
-- Return the result of the function
RETURN
END
2.内联表值函数:返回值为一张表,仅通过一条SQL语句实现,没有逻辑处理能力.可执行大数据量的查询.
代码如下:
--内联表值函数
-- ================================================
-- Template generated from Template Explorer using:
-- Create Inline Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION
(
-- Add the parameters for the function here
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT 0
)
GO
3.多语句表值函数:返回值为一张表,有逻辑处理能力,但仅能对小数据量数据有效,数据量大时,速度很慢.
代码如下:
--多语句表值函数
-- ================================================
-- Template generated from Template Explorer using:
-- Create Multi-Statement Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION
(
-- Add the parameters for the function here
)
RETURNS
TABLE
(
-- Add the column definitions for the TABLE variable here
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
RETURN
END
GO
4.游标:对多条数据进行同样的操作.如同程序的for循环一样.有几种循环方向控制,一般用FETCH Next.
代码如下:
--示意性SQL脚本
DECLARE @MergeDate Datetime
DECLARE @MasterId Int
DECLARE @DuplicateId Int
SELECT @MergeDate = GetDate()
DECLARE merge_cursor CURSOR FAST_FORWARD FOR SELECT MasterCustomerId, DuplicateCustomerId FROM DuplicateCustomers WHERE IsMerged = 0
--定义一个游标对象[merge_cursor]
--该游标中包含的为:[SELECT MasterCustomerId, DuplicateCustomerId FROM DuplicateCustomers WHERE IsMerged = 0 ]查询的结果.
OPEN merge_cursor
--打开游标
FETCH NEXT FROM merge_cursor INTO @MasterId, @DuplicateId
--取数据到临时变量
WHILE @@FETCH_STATUS = 0 --系统@@FETCH_STATUS = 0 时循环结束
--做循环处理
BEGIN
EXEC MergeDuplicateCustomers @MasterId, @DuplicateId
UPDATE DuplicateCustomers
SET
IsMerged = 1,
MergeDate = @MergeDate
WHERE
MasterCustomerId = @MasterId AND
DuplicateCustomerId = @DuplicateId
FETCH NEXT FROM merge_cursor INTO @MasterId, @DuplicateId
--再次取值
END
CLOSE merge_cursor
--关闭游标
DEALLOCATE merge_cursor
--删除游标
[说明:游标使用必须要配对,Open--Close,最后一定要记得删除游标.]
5.事务:当一次处理中存在多个操作,要么全部操作,要么全部不操作,操作失败一个,其他的就全部要撤销,不管其他的是否执行成功,这时就需要用到事务.
代码如下:
begin tran
update tableA
set columnsA=1,columnsB=2
where RecIs=1
if(@@ERROR 0 OR @@ROWCOUNT 1)
begin
rollback tran
raiserror( '此次update表tableA出错!!' , 16 , 1 )
return
end
insert into tableB (columnsA,columnsB) values (1,2)
if(@@ERROR 0 OR @@ROWCOUNT 1)
begin
rollback tran
raiserror( '此次update表tableA出错!!' , 16 , 1 )
return
end
end
commit

tograntpermissionstonewmysqlusers、フォローステープ:1)Accessmysqlasauserwithsufthiveerprivileges、2)createanewuser withthecreateusercommand、3)usethegrantcommandtospecifypermissionsionsionsionsionsionsionsionsionsionsionselect、挿入、挿入、挿入、更新、4)

toadduusersinmysqucrectivally andcurally、soflowthesteps:1)usethecreateuserstatementtoaddanewuser、指定するhostandastrongpassword.2)補助金を使用して、補助金を使用して、補助すること、

toaddanewuserwithpermissionsinmysql、followthesesteps:1)createtheuserwithcreateuser'newuser '@' localhost'identifiedifiedifiedifiedby'pa ssword ';。2)grantreadacestoalltablesin'mydatabase'withgrantselectonmydatabase.to'newuser'@'localhost';。3)grantwriteaccessto '

MySQLの文字列データ型には、CHAR、VARCHAR、バイナリ、Varbinary、BLOB、およびテキストが含まれます。照合は、文字列の比較とソートを決定します。 1.Charは固定長の文字列に適しており、Varcharは可変長文字列に適しています。 2.バイナリとVarbinaryはバイナリデータに使用され、BLOBとテキストは大規模なオブジェクトデータに使用されます。 3. UTF8MB4_UNICODE_CIなどのルールのソートは、高度と小文字を無視し、ユーザー名に適しています。 UTF8MB4_BINは症例に敏感であり、正確な比較が必要なフィールドに適しています。

最適なMySQLVarcharの列の長さの選択は、データ分析に基づいており、将来の成長を検討し、パフォーマンスの影響を評価し、文字セットの要件を評価する必要があります。 1)データを分析して、典型的な長さを決定します。 2)将来の拡張スペースを予約します。 3)パフォーマンスに対する大きな長さの影響に注意してください。 4)ストレージに対する文字セットの影響を考慮します。これらの手順を通じて、データベースの効率とスケーラビリティを最適化できます。

mysqlblobshavelimits:tinyblob(255bytes)、blob(65,535bytes)、mediumblob(16,777,215bytes)、andlongblob(4,294,967,295bytes).tousebl難易度:1)PROFFORMANCESANDSTORERGEBLOBSEXTERNALLY;

MySQLでユーザーの作成を自動化するための最良のツールとテクノロジーには、次のものがあります。1。MySQLWorkBench、中小サイズの環境に適した、使いやすいがリソース消費量が高い。 2。アンシブル、マルチサーバー環境に適した、シンプルだが急な学習曲線。 3.カスタムPythonスクリプト、柔軟性がありますが、スクリプトセキュリティを確保する必要があります。 4。大規模な環境に適した人形とシェフ、複雑ですがスケーラブル。選択する際には、スケール、学習曲線、統合のニーズを考慮する必要があります。

はい、youcansearchinsideablobinmysqlusingspecifictechniques.1)converttheblobtoautf-8stringwithconvert function andsearchusinglike.2)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。
