Solved

Calling a method from sql server 2008: method static

Posted on 2011-03-04
6
270 Views
Last Modified: 2012-08-13
This method 'GetStringFromNumber' is made of. net and I will use my database sql server 2008

 When I declare as follows
 public string GetNumberString (double value)
 displays the following message
 ConvertirNumerosALetra Procedure, Line 1Method, property or the 'GetStringFromNumber'class 'ConvertNumberToText.Utilities' from assembly 'ConvertNumberToText'is not static

 So the solution is
 public static string GetNumberString (double value)

 I want to know why?
0
Comment
Question by:enrique_aeo
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35040097
Can you provide an example of how you are calling it?
0
 

Author Comment

by:enrique_aeo
ID: 35040203
select dbo.DecimalToString(19.98)
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 35083132
the "why" is because you (sql server, actually) need(s) to be able to call the method without needing to initiating an object from the class that holds the method.
it's a "by design" issue.
0
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 150 total points
ID: 35083375
The reason for this behavior is that SQL Server doesn't instantiate the class - it just calls into the code. Since nothing is persisted, there's no way to keep a class reference, so the code needs to be static to ensure that. In your case, the method likely acts only on it's parameters, not relying on any state external to itself, so you're fine to just slap a "static" on there without much thought. It's when you're interacting with other objects in your class or others that are shared that you can run into problems with a poorly planned "static" modifier.

Also, since SQL Server is naturally multi-threaded, it need to ensure that multiple threads can call the method at a time without stepping on each other. When you declare the method static, (to me at least) it means you've thought about the implications of that and that you've built your class appropriately, not using any globals, but keeping all the variables internal to your method, where they're safe for multi-threading.

When you think about the context in which SQL Server is consuming your method, it makes sense, but it confused me at first as well.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now