Published on

July 10, 2013

Best Practices for Posting Data and Code on SQL Server Forums

Like many others, you may have encountered a problem with T-SQL that you need help with. You turn to a popular forum, such as SQLServerCentral.com, hoping to find a solution. However, you may have experienced the frustration of not receiving a timely response or receiving incorrect or incomplete code snippets. So, what went wrong?

Chances are, the way you posted your data or code was not optimal. In this article, we will discuss the wrong and correct ways to post data and code on SQL Server forums to increase your chances of getting a quick and accurate response.

The Wrong Way to Post Data

Typically, when posting data, people start by describing their table and then provide a list of values. However, this format is not suitable for directly inserting into a table for testing purposes. It requires significant formatting and manual effort to convert the data into a usable format. This can discourage potential respondents from helping you or lead to incorrect code suggestions.

The Correct Way to Post Data

To make it easier for others to help you, it is recommended to provide a table definition in code. This approach has several advantages:

  • It clearly identifies the data types and constraints, eliminating unnecessary questions.
  • It provides a precise representation of the table structure.
  • It encourages potential respondents to invest time in solving your problem.

For example, instead of posting a list of values, you can create a temporary table and define its structure using SQL code:

-- If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
    DROP TABLE #mytable

-- Create the test table with 
CREATE TABLE #mytable 
    (
    ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
    DateValue DATETIME,
    Value DECIMAL(9,4),
    YearValue INT,
    MonthValue INT
    )

In addition to the table definition, you can also include any special conditions, such as date formats, that are required for inserting the data.

Posting Data in a Self-Loading Format

Instead of manually typing or copying the data, you can generate a self-loading format using SQL code. This format allows others to easily insert the data into a table for testing purposes. Here’s an example:

SELECT 'SELECT '
  + QUOTENAME(ID,'''')+','
  + QUOTENAME(DateValue,'''')+','
  + QUOTENAME(Value,'''')+','
  + QUOTENAME(YearValue,'''')+','
  + QUOTENAME(MonthValue,'''')
  + ' UNION ALL'
FROM yourtable

Running this code will produce a formatted output that can be directly inserted into the table:

SELECT '4','Oct 17 2007 12:00AM','5.1709','8','1' UNION ALL
SELECT '37','Oct 17 2007 12:00AM','5.5319','17','8' UNION ALL
SELECT '44','Oct 17 2007 12:00AM','5.5793','21','11' UNION ALL
SELECT '54','Oct 17 2007 12:00AM','5.2471','9','2' UNION ALL
SELECT '55','Oct 17 2007 12:00AM','5.1177','7','0' UNION ALL
SELECT '81','Oct 17 2007 12:00AM','5.5510','18','9' UNION ALL
SELECT '86','Oct 17 2007 12:00AM','5.5128','16','7' UNION ALL
SELECT '96','Oct 17 2007 12:00AM','5.5758','20','10' UNION ALL

By modifying the code, you can generate other formats, such as CSV, for different purposes.

Dealing with Data Type Issues

Some data types, like MONEY, do not allow implicit conversions from CHAR or VARCHAR. In such cases, you can modify the code to avoid putting quotes around the data. For example:

SELECT 'SELECT '
  + QUOTENAME(ID,'''')+','
  + QUOTENAME(DateValue,'''')+','
  + CAST( Value AS VARCHAR) +','
  + QUOTENAME(YearValue,'''')+','
  + QUOTENAME(MonthValue,'''')
  + ' UNION ALL'
FROM yourtable

This modified code will generate data that can be inserted into the MONEY data type without any issues.

Conclusion

When posting a question on a SQL Server forum, it is crucial to present your data and code in a format that is easy to understand and use. By following the best practices outlined in this article, you can significantly increase your chances of receiving a quick and accurate response. Remember, making it easy for others to help you will ultimately benefit you in finding a solution to your problem.

So, next time you need assistance with T-SQL, take a few extra minutes to format your data and code properly. Your efforts will be rewarded with timely and effective responses from the SQL Server community.

Happy coding!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.