2

For below script written in .sql files:

if not exists (select * from sys.tables where name='abc_form')
    CREATE TABLE abc_forms (
        x BIGINT IDENTITY, 
        y VARCHAR(60), 
        PRIMARY KEY (x)
    )

Above script has a bug in table name.

For programming languages like Java/C, compiler help resolve most of the name resolutions

For any SQL script, How should one approach unit testing it? static analysis...

6
  • Try removed the “]” at the end Commented Mar 15, 2019 at 13:20
  • @justinmontalban my bad on this Commented Mar 15, 2019 at 13:33
  • Write some batch scripts to run it through the sql plus command line and pipe any ORA errors to an output that you can then look at manually or automatically. Commented Mar 15, 2019 at 14:14
  • @SamM What about adding rules in Sonar using XPATH or Java to perform static code analysis? To avoid manual code(script) reviews Commented Mar 15, 2019 at 14:35
  • @overexchange I'm not familiar with that technology. Are you scripts all pure sql or are they pl/sql? Commented Mar 15, 2019 at 15:56

2 Answers 2

1

15 years ago I did something like you request via a lot of scripting. But we had special formats for the statements.

We had three different kinds of files:

  • One SQL file to setup the latest version of the complete database schema
  • One file for all the changes to apply to older database schema's (custom format like version;SQL)
  • One file for SQL statements the code uses on the database (custom format like statementnumber;statement)

It was required that every statement was on one line so that it could be extracted with awk!

1) At first I set up the latest version of the database by executing from statement after the other and logging the errors to a file.

2) Secondly I did the same for all changes to have a second schema

3) I compared the two database schemas to find any differences

4) I filled in some dummy test values in the complete latest schema for testing

5) Last but not least I executed every SQL statement against the latest schema with test data and logged every error again.

At the end the whole thing runs every night and there was no morning without new errors that one of 20 developers had put into the version control. But it saved us a lot of time during the next install at a new customer.

Sign up to request clarification or add additional context in comments.

Comments

1

You could also generate the SQL scripts from your code.

Code first avoids these kinds of problems. Choosing between code first or database first usually depends on whether your main focus is on your data or on your application.

1 Comment

How generating SQL scripts will resolve this issue? by writing unit test case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.