I had this problem as well. If you want to fix your original python script (instead of the one that the server creates), you can also pass in the name of the sde filename as a parameter. I've pasted my script below. I am using Server 10.22 and it seems to me that the variable substitution is still messed up. Hope this helps
import arcpy, os
#This next line ensures that your .sde file gets copied from whereever you have it #to D:\arcgisserver\directories\arcgissystem\arcgisinput\service_folder\servicename.GPServer\extracted\v101
dbconn = os.path.join(r"D:\arcgisserver\directories\service_scripts", u'DB_NAME.sde')
conn = arcpy.GetParameterAsText(0) #here you pass in your DB_NAME.sde again
#this line will use arcpy.env.packageWorkspace (copied .sde) and the .sde filename
arcpy.env.workspace = os.path.join(r"D:\arcgisserver\directories\service_scripts", conn)
try:
datasets = arcpy.ListDatasets("*", "Feature")
for ds in datasets:
print ds
except arcpy.ExecuteError, ex:
arcpy.AddMessage("An error occurred : " + str(ex))
arcpy.SetParameterAsText(1, "output")