> 데이터 베이스 > MySQL 튜토리얼 > SQL 框架,完全用 SQL 写的

SQL 框架,完全用 SQL 写的

WBOY
풀어 주다: 2016-06-07 14:54:46
원래의
1101명이 탐색했습니다.

无详细内容 无 --Start Customer_SetCREATE PROCEDURE [dbo].[Customer_Set]@Name as varchar(50), @Address as varchar(250), @Mobile as varchar(50)ASBEGINSET NOCOUNT ON--ValidationIF @Name IS NULLBEGINRAISERROR ('Name cannot be empty.',16,1)ENDIF

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

--Start Customer_Set

CREATE PROCEDURE [dbo].[Customer_Set]

    @Name as varchar(50),

        @Address as varchar(250),   

    @Mobile as varchar(50)

AS

BEGIN

 

    SET NOCOUNT ON

 

    --Validation

    IF @Name IS NULL

    BEGIN

        RAISERROR ('Name cannot be empty.',16,1)

    END

 

    IF LEN(@Name)<3

    BEGIN

        RAISERROR ('Name cannot be less than 3 characters.',16,1)

    END

 

    --Data Insertion

    BEGIN TRY

        INSERT INTO [dbo].[Customer]

            ([Name]

            ,[Address]

            ,[Mobile])

        VALUES

            (@Name

            ,@Address          

            ,@Mobile)

    END TRY

    BEGIN CATCH

        RETURN (0)

    END CATCH

 

 RETURN (1)

END

--End Customer_Set

 

 

--Start Supplier_Set

CREATE PROCEDURE [dbo].[Supplier_Set]

@Name as varchar(50),

@Address as varchar(250),   

@Mobile as varchar(50)

AS

BEGIN

 

    SET NOCOUNT ON

 

    --Validation

    IF @Name IS NULL

    BEGIN

        RAISERROR ('Please enter suppiler name.',16,1)

    END

 

    IF LEN(@Name)<3

    BEGIN

        RAISERROR ('Supplier name cannot be less than 3 characters.',16,1)

    END

 

    --Data Insertion

    BEGIN TRY

        INSERT INTO [dbo].[Supplier]

            ([Name]

            ,[Address]

            ,[Mobile])

        VALUES

            (@Name

            ,@Address          

            ,@Mobile)

    END TRY

    BEGIN CATCH

        RETURN (-1)

    END CATCH

 

 RETURN (1)

END

--End Supplier_Set

 

--Start GetValidationConstraint

CRAETE PROCEDURE [dbo].[GetValidationConstraint]

    --Output values

    @EmptyCheck int OUTPUT,

    @LenCheck int  =NULL OUTPUT,

    @NoDataExist int =NULL OUTPUT,

    @True bit =NULL OUTPUT,

    @False bit =NULL OUTPUT

AS

BEGIN

    SELECT @EmptyCheck=1

    SELECT @LenCheck =2

    SELECT @NoDataExist =3

 

    SELECT @True=1 

    SELECT @False=0

END

--End GetValidationConstraint

 

 

--Start ReturnMessage

CREATE PROCEDURE [dbo].[ReturnMessage]

    --Success, Fail is the order of output parameter

    @Success int OUTPUT,   

    @Fail int OUTPUT   

AS

SET NOCOUNT ON

 

BEGIN

    SELECT @Fail=0

    SELECT @Success=1      

END

--End ReturnMessage

 

--Start MessageHelper

CREATE PROCEDURE [dbo].[MessageHelper]

    --Input values

    @Field varchar(200) =NULL,

    @MinLenght int =NULL,

    @ValidationConstraint int,

    --Output values

    @ValidationMessage varchar(200) OUTPUT 

AS

BEGIN

    --Variables

    DECLARE @EMPTY_MESSAGE varchar(50),

            @MINIMUM_LENGHT_MESSAGE varchar(50),

            @NO_DATA_EXIST_MESSAGE varchar(50)

 

 

    DECLARE @EMPTY int,

            @LEN int,

            @NO_DATA_EXIST int 

 

    DECLARE @SUCCESSED int,

            @FAILED int

 

    --Message Constraint

    SET @EMPTY_MESSAGE = 'cannot be empty.'

    SET @MINIMUM_LENGHT_MESSAGE ='cannot be less than'

    SET @NO_DATA_EXIST_MESSAGE = 'No record found.'

     

    --Get global values

    EXEC ReturnMessage @SUCCESSED output, @FAILED output

    EXEC GetValidationConstraint @EMPTY OUTPUT, @LEN OUTPUT, @NO_DATA_EXIST OUTPUT

 

    --Set message

    IF @ValidationConstraint = @EMPTY

    BEGIN

        IF LEN(@Field)<=0

        BEGIN

            RAISERROR('Field name cannot be empty. StoreProcedure/MessageHelper',16,1)     

            RETURN @FAILED

        END

        SELECT @ValidationMessage = @Field + ' ' + @EMPTY_MESSAGE          

    END

 

    IF @ValidationConstraint = @LEN

    BEGIN

        IF @MinLenght IS NULL OR @MinLenght <=0

        BEGIN

            RAISERROR('Minimum length cannot be empty. StoreProcedure/MessageHelper',16,1)     

            RETURN @FAILED

        END

        ELSE

        BEGIN

            SELECT @ValidationMessage = @Field + ' ' + @MINIMUM_LENGHT_MESSAGE + ' ' + CONVERT(varchar, @MinLenght)

        END

    END

 

    IF @ValidationConstraint = @NO_DATA_EXIST

    BEGIN      

        SELECT @ValidationMessage = @NO_DATA_EXIST_MESSAGE         

    END

END

--End MessageHelper

 

--Start Customer_Set

CREATE PROCEDURE [dbo].[Customer_Set]

    --Input values

    @Name as varchar(50),

    @Address as varchar(250),   

    @Mobile as varchar(50),

    --Output values

    @LASTID bigint OUTPUT,

    @MESSAGE varchar(200) =NULL OUTPUT

AS

SET NOCOUNT ON

BEGIN

    --Constraint Variables For Readable Return Value 

    DECLARE @SUCCESSED int,

            @FAILED int        

 

    --Constraint Variables For Readable Validation Operation 

    DECLARE @EMPTY int,

            @LEN int       

     

    BEGIN TRY

        --Get constraint value for successed and failed

        EXEC ReturnMessage @SUCCESSED output, @FAILED output       

        --Get constraint value for validation. @EMPTY is for empty check and @LEN is for length check common messaging system.

        EXEC GetValidationConstraint @EMPTY output, @LEN output

     

        --Validation

        IF LEN(@Name)=0

        BEGIN          

            EXEC MessageHelper 'Name', @EMPTY,@MESSAGE OUTPUT --It will generate a common empty message.

            RETURN @FAILED-- Readable Failed Return

        END

 

        IF LEN(@Name)<3

        BEGIN          

            EXEC MessageHelper 'Name',3, @LEN,@MESSAGE OUTPUT --It will generate a common length check message.

            RETURN @FAILED-- Readable Failed Return

        END

         

        --Data insertion

        INSERT INTO [dbo].[Customer]

           ([Name]

           ,[Address]

           ,[Mobile])

        VALUES

           (@Name

           ,@Address

           ,@Mobile)

             

            SELECT @LASTID=SCOPE_IDENTITY()

    END TRY

    BEGIN CATCH -- Error Traping Section

        DECLARE @ErrorMessage nvarchar(4000);

        DECLARE @ErrorSeverity int;

        DECLARE @ErrorState int;

         

        SELECT

            @ErrorMessage = ERROR_MESSAGE(),

            @ErrorSeverity = ERROR_SEVERITY(),

            @ErrorState = ERROR_STATE();

         

        RAISERROR (@ErrorMessage,@ErrorSeverity,@ErrorState);

        RETURN @FAILED  -- Readable Failed Return

    END CATCH

 

    RETURN @SUCCESSED -- Readable Successed Return

END

--End Customer_Set

 

--Start Customer_Get

CREATE PROCEDURE [dbo].[Customer_Get]  

    --Output values

    @TOTAL_ROWS bigint OUTPUT

    @MESSAGE varchar(200) =NULL OUTPUT

     

AS

BEGIN

    SET NOCOUNT ON

    --Variables

    DECLARE @SUCCESSED int,

            @FAILED int        

 

    DECLARE @EMPTY int,

            @LEN int,

            @NO_DATA_EXIST int         

     

    BEGIN TRY

        --Get constraint value

        EXEC ReturnMessage @SUCCESSED OUTPUT, @FAILED OUTPUT                       

        EXEC GetValidationConstraint @EMPTY OUTPUT, @LEN OUTPUT,@NO_DATA_EXIST OUTPUT

     

        --Validation

        IF (SELECT COUNT(CustomerId) FROM  Customer )<= 0

        BEGIN          

            EXEC MessageHelper '', @NO_DATA_EXIST,@MESSAGE OUTPUT --It will generate common no data exist message.

            SELECT @TOTAL_ROWS=0

              

            RETURN @SUCCESSED

        END

         

        --Data retrival

        SELECT [CustomerId]

          ,[Name]

          ,[Address]

          ,[Mobile]      

        FROM [dbo].[Customer]          

 

        --Get total rows   

        SELECT @TOTAL_ROWS=@@ROWCOUNT

          

    END TRY

    BEGIN CATCH

        DECLARE @ErrorMessage nvarchar(4000);

        DECLARE @ErrorSeverity int;

        DECLARE @ErrorState int;

         

        SELECT

            @ErrorMessage = ERROR_MESSAGE(),

            @ErrorSeverity = ERROR_SEVERITY(),

            @ErrorState = ERROR_STATE();       

         

        RAISERROR (@ErrorMessage,@ErrorSeverity,@ErrorState);

        RETURN @FAILED

    END CATCH

 

    RETURN @SUCCESSED

END

--End Customer_Get

 

--Start Customer_DeleteById

CREATE  PROCEDURE [dbo].[Customer_DeleteById]

    --Input values

    @CustomerId bigint,

    @MESSAGE varchar(200) =NULL OUTPUT

AS

BEGIN

    --Variables

    DECLARE @SUCCESSED int,

            @FAILED int        

 

    DECLARE @EMPTY int,

            @LEN int

 

    BEGIN TRY

        --Get constraint value

        EXEC ReturnMessage @SUCCESSED OUTPUT, @FAILED OUTPUT       

        EXEC GetValidationConstraint @EMPTY OUTPUT, @LEN OUTPUT

     

        --Validation

        IF @@CustomerId <=0

        BEGIN          

            EXEC MessageHelper 'Customer Id', @EMPTY,@MESSAGE OUTPUT

            RETURN @FAILED

        END

         

        --Data deletion

        DELETE FROM [dbo].[Customer]

          WHERE (CustomerId = @CustomerId)

 

    END TRY

    BEGIN CATCH

        DECLARE @ErrorMessage nvarchar(4000);

        DECLARE @ErrorSeverity int;

        DECLARE @ErrorState int;

         

        SELECT

            @ErrorMessage = ERROR_MESSAGE(),

            @ErrorSeverity = ERROR_SEVERITY(),

            @ErrorState = ERROR_STATE();

         

        RAISERROR (@ErrorMessage,@ErrorSeverity,@ErrorState);

        RETURN @FAILED

    END CATCH

 

    RETURN @SUCCESSED

END

 

--End Customer_DeleteById

로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿