‘Must Declare the Scalar Variable’ Error When Passing a Table-Valued Parameter to a Parameterized SQL Statement

First things first: I have no idea where you are getting the tableName and columnName, but if they are user-supplied, then this is open to SQL injection. At the very least, use QUOTENAME() to ensure no actual code is injected. Secondly, you are not actually using the TVP. The code you have is just saying … Read more

Ambiguous column name error

Because ARTIFACTTYPE can refer to either A.ARTIFACTTYPE or B.ARTIFACTTYPE and the server needs to know which one you want, just change it to A.ARTIFACTTYPE and you should be okay in this case. To clarify, you need to specify the alias prefix any time the column name is ambiguous. It isn’t bad practice to always use … Read more

How to throw a SqlException when needed for mocking and unit testing?

I have a solution to this. I’m not sure whether it’s genius or madness. The following code will create a new SqlException: public SqlException MakeSqlException() { SqlException exception = null; try { SqlConnection conn = new SqlConnection(@”Data Source=.;Database=GUARANTEED_TO_FAIL;Connection Timeout=1″); conn.Open(); } catch(SqlException ex) { exception = ex; } return(exception); } which you can then use … Read more

ResultSet.getString(1) throws java.sql.SQLException: Invalid operation at current cursor position

You should use the next statement first. ResultSet set = statement.executeQuery(); if (set.next()) { userName = set.getString(1); //your logic… } UPDATE As the Java 6 Documentation says A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes … Read more

conversion of a varchar data type to a datetime data type resulted in an out-of-range value

Ambiguous date formats are interpreted according to the language of the login. This works set dateformat mdy select CAST(’03/28/2011 18:03:40′ AS DATETIME) This doesn’t set dateformat dmy select CAST(’03/28/2011 18:03:40′ AS DATETIME) If you use parameterised queries with the correct datatype you avoid these issues. You can also use the unambiguous “unseparated” format yyyyMMdd hh:mm:ss

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0

If you have a TRY/CATCH block then the likely cause is that you are catching a transaction abort exception and continue. In the CATCH block you must always check the XACT_STATE() and handle appropriate aborted and uncommitable (doomed) transactions. If your caller starts a transaction and the calee hits, say, a deadlock (which aborted the … Read more

java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x91\xBD\xF0\x9F…’

What you have is EXTRATERRESTRIAL ALIEN (U+1F47D) and BROKEN HEART (U+1F494) which are not in the basic multilingual plane. They cannot be even represented in java as one char, “👽💔”.length() == 4. They are definitely not null characters and one will see squares if you are not using fonts that support them. MySQL’s utf8 only … Read more